(四) Java多线程详解之线程池的使用

来源:互联网 发布:软件生命周期 编辑:程序博客网 时间:2024/06/05 11:34

1.关于线程池
线程池作用就是限制系统中执行线程的数量,根据系统的环境情况,可以自动或手动设置线程数量,达到运行的最佳效果,少了浪费了系统资源,多了造成系统拥挤效率不高。用线程池控制线程数量,其他线程排队等候。一个任务执行完毕,再从队列的中取最前面的任务开始执行。

在线程池的编程模式下,任务是提交给整个线程池,而不是直接交给某个线程,线程池在拿到任务后,它就在内部找有无空闲的线程,再把任务交给内部某个空闲的线程,这就是封装。任务是提交给整个线程池,一个线程同时只能执行一个任务,但可以同时向一个线程池提交多个任务。

2.四种不同的线程池
2.1 newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,超出的线程会在队列中等待。示例代码如下:

public class ThreadExample8 {    public static void main(String[] args) {        ExecutorService threadPool = Executors.newFixedThreadPool(3);        for (int i = 1; i <= 10; i++) {            final int task = i;            threadPool.execute(new Runnable() {                @Override                public void run() {                    for (int j = 1; j <= 10; j++) {                        try {                            Thread.sleep(20);                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                        System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for  task of " + task);                    }                }            });        }        System.out.println("all of 10 tasks have committed!");    }}

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

public class ThreadExample9 {    public static void main(String[] args) {        ExecutorService threadPool = Executors.newCachedThreadPool();        for (int i = 1; i <= 10; i++) {            final int task = i;            threadPool.execute(new Runnable() {                @Override                public void run() {                    for (int j = 1; j <= 10; j++) {                        try {                            Thread.sleep(20);                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                        System.out.println(Thread.currentThread().getName() + " is looping of " + j + " for  task of " + task);                    }                }            });        }        System.out.println("all of 10 tasks have committed!");    }}

2.3 newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。示例代码如下:

public class ThreadExample10 {    public static void main(String[] args) {        ExecutorService threadPool = Executors.newSingleThreadExecutor();        for (int i = 1; i <= 10; i++) {            final int task = i;            threadPool.execute(new Runnable() {                @Override                public void run() {                    for (int j = 1; j <= 10; j++) {                        try {                            Thread.sleep(20);                        } catch (InterruptedException e) {                            e.printStackTrace();                        }                        System.out.println(Thread.currentThread().getName() + " i s looping of " + j + " for  task of " + task);                    }                }            });        }        System.out.println("all of 10 tasks have committed!");    }}

2.4 newScheduledThreadPool 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。示例代码如下:

public class ThreadExample11 {    public static void main(String[] args) {        Executors.newScheduledThreadPool(3).scheduleAtFixedRate(new Runnable() {            @Override            public void run() {                System.out.println("定时任务线程池");            }        }, 5, 3, TimeUnit.SECONDS);    }}
原创粉丝点击