小记录2-工作

{
   // 扎到一个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());
    }
}

 

转载于:https://my.oschina.net/Business/blog/910017

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值