java线程池的应用示例

线程池的编写需要有一定的代码基础,本着不重复造轮子的思想,我们可以采用jdk1.5及以后的相关版本给我们提供的线程池。

Java里面线程池的顶级接口是Executor,但是严格意义上讲Executor并不是一个线程池,而只是一个执行线程的工具。真正的线程池接口是ExecutorService。java线程池的类体系结构

https://i-blog.csdnimg.cn/blog_migrate/81da7c8ceb63024326efef3f83389f27.png

首先Executor的execute方法只是执行一个Runnable的任务,当然了从某种角度上将最后的实现类也是在线程中启动此任务的。根据线程池的执行策略最后这个任务可能在新的线程中执行,或者线程池中的某个线程,甚至是调用者线程中执行(相当于直接运行Runnable的run方法)。

ExecutorService:真正的线程池接口。
ScheduledExecutorService能和Timer/TimerTask类似,解决那些需要任务重复执行的问题。
ThreadPoolExecutorExecutorService的默认实现。
ScheduledThreadPoolExecutor继承ThreadPoolExecutor的ScheduledExecutorService接口实现,周期性任务调度的类实现。

在java的API中,Executors类提供了一些静态工厂方法,生成一些常用的线程池,这个也是API中推荐我们使用的。

  • newSingleThreadExecutor:创建一个单线程的线程池。这个线程池只有一个线程在工作,也就是相当于单线程串行执行所有任务。如果这个唯一的线程因为异常结束,那么会有一个新的线程来替代它。此线程池保证所有任务的执行顺序按照任务的提交顺序执行。
  • newFixedThreadPool:创建固定大小的线程池。每次提交一个任务就创建一个线程,直到线程达到线程池的最大大小。线程池的大小一旦达到最大值就会保持不变,如果某个线程因为执行异常而结束,那么线程池会补充一个新线程。
  • newCachedThreadPool:创建一个可缓存的线程池。如果线程池的大小超过了处理任务所需要的线程,那么就会回收部分空闲(60秒不执行任务)的线程,当任务数增加时,此线程池又可以智能的添加新线程来处理任务。此线程池不会对线程池大小做限制,线程池大小完全依赖于操作系统(或者说JVM)能够创建的最大线程大小。
  • newScheduledThreadPool:创建一个大小无限的线程池,此线程池支持定时以及周期性执行任务的需求。

下面举例前三种线程池的应用效果

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class MyThreadPool {
	public static void main(String[] args) {

//		ExecutorService pool = Executors.newSingleThreadExecutor();//单线程串行执行
		ExecutorService pool = Executors.newFixedThreadPool(3); // 创建一个可重用固定线程数的线程池
//		ExecutorService pool = Executors.newCachedThreadPool();//无界线程池,可以进行自动线程回收

		// 创建实现了Runnable接口对象或者Thread类的对象
		Thread t1 = new MyThread("t1");
		Thread t2 = new MyThread("t2");
		Thread t3 = new MyThread("t3");
		Thread t4 = new MyThread("t4");
		Thread t5 = new MyThread("t5");
		System.out.println("Thread start execute ... ");
		// 将线程放入池中进行执行
		pool.execute(t1);
		pool.execute(t2);
		pool.execute(t2);
		pool.execute(t2);
		pool.execute(t2);
		pool.execute(t3);
		pool.execute(t4);
		pool.execute(t5);

		// 关闭线程池
		pool.shutdown();
		// 线程池关闭后,如果还有线程在执行,那么主线程需等待下
		while (!pool.isTerminated()) {
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println("All thread over ");
	}

}

class MyThread extends Thread {
	private String name = "";

	public MyThread(String name) {
		this.name = name;
	}

	@Override
	public void run() {
		System.out.println(Thread.currentThread().getName() + "\t"+this.name+" execute ... ");
	}
}

(1) newSingleThreadExecutor线程池开启的执行结果

Thread start execute ...
pool-1-thread-1    t1 execute ...
pool-1-thread-1    t2 execute ...
pool-1-thread-1    t2 execute ...
pool-1-thread-1    t2 execute ...
pool-1-thread-1    t2 execute ...
pool-1-thread-1    t3 execute ...
pool-1-thread-1    t4 execute ...
pool-1-thread-1    t5 execute ...
All thread over

整个执行过程中只有pool-1-thread-1一个线程在处理任务

(2) newFixedThreadPool(3) 开启结果

Thread start execute ...
pool-1-thread-1    t1 execute ...
pool-1-thread-2    t2 execute ...
pool-1-thread-2    t2 execute ...
pool-1-thread-2    t2 execute ...
pool-1-thread-2    t3 execute ...
pool-1-thread-2    t4 execute ...
pool-1-thread-2    t5 execute ...
pool-1-thread-3    t2 execute ...
All thread over
整个执行过程中有3个线程在任务。

(3) newCachedThreadPool 执行结果

Thread start execute ...
pool-1-thread-2    t2 execute ...
pool-1-thread-4    t2 execute ...
pool-1-thread-6    t3 execute ...
pool-1-thread-8    t5 execute ...
pool-1-thread-1    t1 execute ...
pool-1-thread-3    t2 execute ...
pool-1-thread-5    t2 execute ...
pool-1-thread-7    t4 execute ...
All thread over
整个执行过程中有8个线程在处理任务。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值