观察者 模式 和 线程池的应用

1.线程池的使用

核心对象解释:

ThreadPoolExecutor executor = new ThreadPoolExecutor(corePoolSize,maximumPoolSize, keepAliveTime,TimeUnit.SECONDS,newLinkedBlockingQueue<Runnable>(),       

Executors.defaultThreadFactory(), new AbortPolicy());

参数意义:
参1:核心线程数; 参2:最大线程数;
参3:线程休眠时间; 参4:时间单位;
参5:线程队列; 参6:生产线程的工厂;
参7:线程异常处理策略

解释:
    executor这个对象声明,直译为:线程池的执行者,该对象可以对线程池中的线程进行管理;  使用时填好几个参数即可
  • public void execute(Runnable command);
    这个方法可以直接执行Runnable方法,自动将Runnable 对象
    加入到线程池中 的线程中(至于加到哪个线程,由线程池内部机
    制决定);

事例代码:

/**
     * @Description: 线程管理类
     * @author: penghao
     * @date: 2016-11-5 下午3:26:36
     */
    public class ThreadManager {

        private static ThreadPool mThredPool = null;

        // 懒汉式
        public static ThreadPool getThreadPool() {
            if (mThredPool == null) {
                synchronized (ThreadManager.class) {
                    if (mThredPool == null) {
                        int count = Runtime.getRuntime().availableProcessors();
                        System.out.println("当前手机的cpu  核心数为:" + count);

                        // 默认最大 开十个 线程                      睡眠时间为1秒
                        int threadCount = 10;
                        mThredPool = new ThreadPool(threadCount, threadCount, 1L);
                    }
                }
            }
            return mThredPool;
        }

        /**
         * @Description: 线程池
         * @author: penghao
         * @date: 2016-11-5 下午3:41:58
         */
        public static class ThreadPool {
            private final int corePoolSize; // 核心线程数
            private int maximumPoolSize; // 最大线程数
            private long keepAliveTime; // 休息时间
            private ThreadPoolExecutor executor;

            public ThreadPool(int corePoolSize, int maximumPoolSize,
                    long keepAliveTime) {
                this.corePoolSize = corePoolSize;
                this.maximumPoolSize = maximumPoolSize;
                this.keepAliveTime = keepAliveTime;
            }

            /**
             * @Description: TODO 执行      线程中的 Runnable 对象,
             * @param r
             */
            public void execute(Runnable r) {
                if (executor == null) {
                    executor = new ThreadPoolExecutor(corePoolSize,
                            maximumPoolSize, keepAliveTime, TimeUnit.SECONDS,
                            new LinkedBlockingQueue<Runnable>(),
                            Executors.defaultThreadFactory(), new AbortPolicy());

                    // 参1:核心线程数;参2:最大线程数;参3:线程休眠时间;参4:时间单位;参5:线程队列;参6:生产线程的工厂;参7:线程异常处理策略
                }

                // 线程池执行一个Runnable对象, 具体运行时机线程池说了算
                executor.execute(r);
            }

            public void cancel(Runnable r) {
                if (executor != null) {
                    // 从线程队列中移除 对象
                    executor.getQueue().remove(r);
                }
            }
        }

    }

2.观察者设计模式

<img src="picture/observermodel.png"/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值