0308线程池

线程池:三大方法、7大参数、4种拒绝策略

线程池的好处:
1、降低资源的消耗
2、提高响应的速度
3、方便管理。
线程复用、可以控制最大并发数、管理线程

三大方法

ExecutorService threadPool = Executors.newSingleThreadExecutor();// 单个线程
ExecutorService threadPool = Executors.newFixedThreadPool(5); // 创建一个固定的线程池的大小
ExecutorService threadPool = Executors.newCachedThreadPool(); // 可伸缩的,遇强则强,遇弱则弱

例子:

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
// Executors 工具类、3大方法
public class Demo01 {
		public static void main(String[] args) {
			ExecutorService threadPool = Executors.newSingleThreadExecutor();// 单个线程
			// ExecutorService threadPool = Executors.newFixedThreadPool(5); // 创建一个固定的线程池的大小
			// ExecutorService threadPool = Executors.newCachedThreadPool(); // 可伸缩的,遇强则强,遇弱则弱
			try {
				for (int i = 0; i < 100; i++) {
					// 使用了线程池之后,使用线程池来创建线程
					threadPool.execute(()->{
						System.out.println(Thread.currentThread().getName()+" ok");
				});
			}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				// 线程池用完,程序结束,关闭线程池
				threadPool.shutdown();
		}
	}
}
ExecutorService threadPool = Executors.newSingleThreadExecutor();// 单个线程
ExecutorService threadPool = Executors.newFixedThreadPool(5); // 创建一个固定的线程池的大小
ExecutorService threadPool = Executors.newCachedThreadPool(); // 可伸缩的,遇强则强,遇弱则弱

7大参数

源码:

public static ExecutorService newSingleThreadExecutor() {
	return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>()));
	//核心线程数:1
	//最大线程池大小:1
	//超时0释放
	//单位:MILLISECONDS
	//阻塞队列LinkedBlockingQueue
}
public static ExecutorService newFixedThreadPool(int nThreads) {
	return new ThreadPoolExecutor(5, 5,	0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool() {
	return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
}

本质是调用ThreadPoolExecutor

ThreadPoolExecutor源码:

public ThreadPoolExecutor(int corePoolSize, // 核心线程池大小
	int maximumPoolSize, // 最大核心线程池大小
	long keepAliveTime, //同步时间。 超时了没有人调用就会释放
	TimeUnit unit, // 超时单位
	BlockingQueue<Runnable> workQueue, // 阻塞队列
	ThreadFactory threadFactory, // 线程工厂:创建线程的,一般不用动
	RejectedExecutionHandler handle)// 拒绝策略
	{
		if (corePoolSize < 0 ||	maximumPoolSize <= 0 ||	maximumPoolSize < corePoolSize ||	keepAliveTime < 0)
			throw new IllegalArgumentException();
		if (workQueue == null || threadFactory == null || handler == null)
			throw new NullPointerException();
		this.acc = System.getSecurityManager() == null ?null :	AccessController.getContext();
		this.corePoolSize = corePoolSize;
		this.maximumPoolSize = maximumPoolSize;
		this.workQueue = workQueue;
		this.keepAliveTime = unit.toNanos(keepAliveTime);
		this.threadFactory = threadFactory;
		this.handler = handler;
}

4种拒绝策略

/**
* new ThreadPoolExecutor.AbortPolicy() // 银行满了,还有人进来,不处理这个人的,抛出异
常
* new ThreadPoolExecutor.CallerRunsPolicy() // 哪来的去哪里!
* new ThreadPoolExecutor.DiscardPolicy() //队列满了,丢掉任务,不会抛出异常!
* new ThreadPoolExecutor.DiscardOldestPolicy() //队列满了,尝试去和最早的竞争,也不会
抛出异常!
*/

例子:

import java.util.concurrent.*;
public class Demo01 {
	public static void main(String[] args) {
		// 自定义线程池!工作 ThreadPoolExecutor
		// 最大线程到底该如何定义
		// 1、CPU 密集型,几核,就是几,可以保持CPu的效率最高!
		// 2、IO 密集型 > 判断你程序中十分耗IO的线程,
		// 程序 15个大型任务 io十分占用资源!
		// 获取CPU的核数
		System.out.println(Runtime.getRuntime().availableProcessors());
		ExecutorService threadPool = new ThreadPoolExecutor(
			2,
			Runtime.getRuntime().availableProcessors(),
			3,
			TimeUnit.SECONDS,
			new LinkedBlockingDeque<>(3),
			Executors.defaultThreadFactory(),
			new ThreadPoolExecutor.DiscardOldestPolicy()); 
			//队列满了,尝试去和最早的竞争,也不会抛出异常!
		try {
		// 最大承载:Deque + max
		// 超过 RejectedExecutionException
			for (int i = 1; i <= 9; i++) {
				// 使用了线程池之后,使用线程池来创建线程
				threadPool.execute(()->{
				System.out.println(Thread.currentThread().getName()+" ok");
				});
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 线程池用完,程序结束,关闭线程池
			threadPool.shutdown();
		}
	}
}
内容概要:本文介绍了基于Proteus利用SR锁存器74LS279和与非门74LS02D设计抢答器的方法。文章详细讲解了通过SR锁存器的高、低电平状态实现抢答锁定功能,即S低电平置高电平用于抢答,R低电平置低电平用于清零,确保抢答按键按下后无法再次触发。抢答结果通过数码管显示,其中1、2、3序号分别对应不同的按键,4路抢答器独立显示序号,3路抢答器则通过或逻辑处理并列抢答的情况,如1和2、1和3、2和3序号并列抢答时显示“3”,并用LED指示具体并列情况。电路还集成了声光反馈机制,当抢答有效时,蜂鸣器发出声音提示。此外,文章提到可通过三刀双掷开关切换是否允许并列抢答,以及通过锁存器实现抢答器路数的扩展,并提出加入倒计时功能进一步完善系统。 适合人群:对数字电路设计有一定兴趣的电子爱好者,尤其是希望深入了解锁存器应用及抢答器设计原理的初学者和中级工程师。 使用场景及目标:①学习如何利用SR锁存器实现抢答器的基本功能;②掌握并列抢答情况下的电路处理方法;③理解数码管、LED、蜂鸣器等元件在电路中的应用;④探索电路的可扩展性和改进方向。 阅读建议:读者应结合Proteus软件进行实际电路仿真,动手搭建文中所述电路,逐步理解每个组件的作用及其相互关系,同时关注电路中可能出现的问题及解决方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值