zoukankan      html  css  js  c++  java
  • 一些性能优化的tips

    工作中积累的一些性能优化的tips,记录一下:

    1. Message的创建

    Message message = Message.obtain();  // 推荐

    Message message = new Message();  // 不推荐

    原因:message在使用完之后对调用recycle,这个操作会将message放入pool中,而obtain()方法通过从全局pool中获取已有对象创建Message,从而避免创建大量的对象

    public static Message obtain() {
        synchronized (sPoolSync) {
            if (sPool != null) {
                Message m = sPool;
                sPool = m.next;
                m.next = null;
                m.flags = 0; // clear in-use flag
                sPoolSize--;
                return m;
            }
         }
         return new Message();
    }

    2. 单例模式

    例:子线程中避免重复创建handler,可以采用单例模式,保证handler只有一个实例

    private Handler mHandler;
    
    private static class MHandler extends Handler {
        public MHandler(Looper looper) {
            super(looper);
        }
    
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    }
    
    public Handler getHandler() {
        if (mHandler == null) {
            synchonized(MyHandler.class) {
                mHandler = new MyHandler();
            }
        }
        return mHandler;    
    } 

    3. 线程池

     受限于硬件、内存和性能,我们不可能无限制的创建任意数量的线程,因为每一台机器允许的最大线程是一个有界值。

    也就是说ThreadPoolExecutor管理的线程数量是有界的。线程池就是用这些有限个数的线程,去执行提交的任务。

    使用newFixedThreadPool()可以创建固定大小的线程池,可控制线程最大并发数,超出的线程会在队列中等待。

    private static ExecutorService executorService = Executors.newFixedThreadPool(4);
    
    private doWork() { executorService.execute(
    new Runnable() { @Override public void run() { Message message = Message.obtain(); message.obj = onDoInBackground(); getHandler().sendMessage(message); } });
    }

    4. 

  • 相关阅读:
    力扣leetcode 435. 无重叠区间
    C++中的快读和快写
    python安装wordcloud库报错
    python习题 随机密码生成 + 连续质数计算
    python 四位玫瑰数 + 100以内素数求和
    解决git add README.md 时报错 fatal: pathspec 'README.md' did not match any files
    洛谷P3817 小A的糖果 贪心思想
    多机部署之session共享解决方案-copy
    一点区块链资料-copy
    kafka集群搭建及结合springboot使用-copy
  • 原文地址:https://www.cnblogs.com/nicoleTeng/p/7680504.html
Copyright © 2011-2022 走看看