Java面试

多线程2,线程池深入理解

2018-04-17  本文已影响33人  杨充211

目录介绍

前言介绍

1.ThreadPoolExecutor类介绍

1.1 构造函数

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue) {
    this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
         Executors.defaultThreadFactory(), defaultHandler);
}

1.2 参数解析

1.3 遵循的规则

1.4 使用线程池管理线程的优点

2.关于线程池的分类

2.1 FixedThreadPool

2.2 CachedThreadPool

2.3 ScheduledThreadPool

2.4 SingleThreadExecutor

3.线程池一般用法

3.1 一般方法介绍

3.2 newFixedThreadPool的使用

private void newFixedThreadPool() {
    ExecutorService fixedThreadPool = Executors.newFixedThreadPool(5);
    for (int i = 1; i <= 20; i++) {
        final int index = i;
        fixedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                String threadName = Thread.currentThread().getName();
                Log.e("潇湘剑雨", "线程:"+threadName+",正在执行第" + index + "个任务");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

3.3 newSingleThreadExecutor的使用

private void newSingleThreadExecutor() {
    ExecutorService singleThreadPool = Executors.newSingleThreadExecutor();
    for (int i = 1; i <= number; i++) {
        final int index = i;
        singleThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                String threadName = Thread.currentThread().getName();
                Log.v("潇湘剑雨", "线程:"+threadName+",正在执行第" + index + "个任务");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

3.4 newCachedThreadPool的使用

private void newCachedThreadPool() {
    ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
    for (int i = 1; i <= number; i++) {
        final int index = i;
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        cachedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                String threadName = Thread.currentThread().getName();
                Log.v("潇湘剑雨newCachedThreadPool", "线程:" + threadName + ",正在执行第" + index + "个任务");
                try {
                    long time = index * 500;
                    Thread.sleep(time);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

3.5 newScheduledThreadPool的使用

private void newScheduledThreadPool() {
    ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
    //延迟2秒后执行该任务
    scheduledThreadPool.schedule(new Runnable() {
        @SuppressLint("LongLogTag")
        @Override
        public void run() {
            String threadName = Thread.currentThread().getName();
            Log.e("潇湘剑雨newScheduledThreadPool", "线程:" + threadName + ",正在执行");
        }
    }, 2, TimeUnit.SECONDS);
    //延迟1秒后,每隔2秒执行一次该任务
    scheduledThreadPool.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            String threadName = Thread.currentThread().getName();
            Log.e("潇湘剑雨", "线程:" + threadName + ",正在执行");
        }
    }, 1, 2, TimeUnit.SECONDS);
}

3.6 线程创建规则

4.线程池封装

4.1 具体可以参考下篇文章

4.2 参考博客

上一篇 下一篇

猜你喜欢

热点阅读