thread pool线程池

时间:2020-09-18 00:05:40   收藏:0   阅读:27

在面向对象编程中,创建和销毁对象是很费时间的,因为创建一个对象要获取内存资源或者其它更多资源。在Java中更是如此,虚拟机将试图跟踪每一个对象,以便能够在对象销毁后进行垃圾回收。所以提高服务程序效率的一个手段就是尽可能减少创建和销毁对象的次数,特别是一些很耗资源的对象创建和销毁,这就是”池化资源”技术产生的原因。线程池顾名思义就是事先创建若干个可执行的线程放入一个池(容器)中,需要的时候从池中获取线程不用自行创建,使用完毕不需要销毁线程而是放回池中,从而减少创建和销毁线程对象的开销。

  1. Java 5+中的Executor接口定义一个执行线程的工具。
    它的子类型即线程池接口是ExecutorService。要配置一个线程池是比较复杂的,尤其是对于线程池的原理不是很清楚的情况下,因此在工具类Executors面提供了一些静态工厂方法,生成一些常用的线程池,如下所示:

如果希望在服务器上使用线程池,建议使用newFixedThreadPool方法来创建线程池,这样能获得更好的性能。

1. newSingleThreadExecutor()

创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
我们可以观察到,控制台的打印输出的线程名都是一样的。

public static void main(String[] args) {
    ExecutorService executorService = Executors.newSingleThreadExecutor();

    for (int i = 0; i < 10; i++) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i < 100; i++){
                    System.out.println(Thread.currentThread().getName()+"="+i);
                }
            }
        });
    }
    
}

2. newFixedThreadPool(100);

创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。

public static void main(String[] args) {
    ExecutorService executorService = Executors.newFixedThreadPool(100);

    for (int i = 0; i < 10; i++) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i < 100; i++){
                    System.out.println(Thread.currentThread().getName()+"="+i);
                }
            }
        });
    }
    
}

3. newCachedThreadPool()

创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。

public static void main(String[] args) {
    ExecutorService executorService = Executors.newCachedThreadPool();

    for (int i = 0; i < 10; i++) {
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i < 100; i++){
                    System.out.println(Thread.currentThread().getName()+"="+i);
                }
            }
        });
    }
    
}

4. newScheduledThreadPool

创建一个定长线程池,支持定时及周期性任务执行。

public static void main(String[] args) {
    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);

    for (int i = 0; i < 10; i++) {
        scheduledExecutorService.schedule(new Runnable() {
            @Override
            public void run() {
                for(int i = 0; i < 100; i++){
                    System.out.println(Thread.currentThread().getName()+"="+i);
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },3, TimeUnit.SECONDS);
    }
}

打印输出,每隔三秒输出一次,每次都输出另外一个线程2121212121

pool-1-thread-2=0
pool-1-thread-1=0
pool-1-thread-2=1
pool-1-thread-1=1
pool-1-thread-1=2
pool-1-thread-2=2
pool-1-thread-1=3
pool-1-thread-2=3
pool-1-thread-1=4
pool-1-thread-2=4
pool-1-thread-1=5
pool-1-thread-2=5
pool-1-thread-1=6
pool-1-thread-2=6
pool-1-thread-1=7
pool-1-thread-2=7
pool-1-thread-1=8
pool-1-thread-2=8
pool-1-thread-1=9
pool-1-thread-2=9
pool-1-thread-1=10
pool-1-thread-2=10
pool-1-thread-2=11
pool-1-thread-1=11
pool-1-thread-2=12
pool-1-thread-1=12


博客参照:

https://www.cnblogs.com/zhaoyan001/p/7049627.html
https://blog.csdn.net/jackfrued/article/details/44921941 ,第65题

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!