{
// 扎到一个class的class path
public static String findClassPath(Class<?> c) {
URL url = c.getProtectionDomain().getCodeSource().getLocation();
return url.getPath();
}
}
/**
* 安全的写文件,来自rocketmq Mixall.class
*/
public static final void string2File(final String str, final String fileName) throws IOException {
// 先写入临时文件
String tmpFile = fileName + ".tmp";
string2FileNotSafe(str, tmpFile); // 简单的实现函数
// 备份之前的文件
String bakFile = fileName + ".bak";
String prevContent = file2String(fileName); //从之前的文件读出来
if (prevContent != null) {
string2FileNotSafe(prevContent, bakFile); //再写进去
}
// 删除正式文件
File file = new File(fileName);
file.delete();
// 临时文件改为正式文件
file = new File(tmpFile);
file.renameTo(new File(fileName));
}
{
//尽量不要使用contains("songlei") 多使用indexof("songlei") >= 0
}
{
//线程池大小控制
private int sendMessageThreadPoolNums = 16 + Runtime.getRuntime().availableProcessors() * 4;
private int pullMessageThreadPoolNums = 16 + Runtime.getRuntime().availableProcessors() * 2;
}
{
syncronised(this) {
notify(); // or notifyAll();
}
syncronised(this) {
wait(); // wait 2
}
syncronised (this) {
wait(); // wait 1
}
}
{
thread.setDeamon(true) //在它下面子线程全部执行完成后 它自动完成 否则就一直守护着静静的在背后执行
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable, "System Clock");
thread.setDaemon(true);
return thread;
}
});
}
{
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean(); // 获取java 进程
String name = runtime.getName(); // format: "pid@hostname" //得到此进程的id和名字
}
{
Character.isWhitespace(str.charAt(i) //判断是否有空格
}
{
private static byte charToByte(char c) { // 将char转化成byte
return (byte) "0123456789ABCDEF".indexOf(c);
}
}
{
// 让程序一开始就加载这些
public static void init() {
// 定义需要加载的 Helper 类
Class<?>[] classList = {
DatabaseHelper.class,
EntityHelper.class,
ActionHelper.class,
BeanHelper.class,
AopHelper.class,
IocHelper.class, // 自动加载的代码实现在静态块里面
PluginHelper.class,
};
for (Class<?> cls : classList) { // 按照顺序加载类
ClassUtil.loadClass(cls.getName()); // Class.forName(className, isInitialized,getClassLoader());
}
}