JAVA-----线程池

仅提供学习,侵权必删,如有错误,敬请告知

一、ThreadPoolExecutor

package jihe;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class MyThreadPool {
	public static void main(String[] args) {
		ThreadPoolExecutor executor = new ThreadPoolExecutor(
				5,  	//初始线程数
				15, 	//最大线程数
				60, 	//时间数
				TimeUnit.SECONDS,//时间单位
				new LinkedBlockingQueue<Runnable>(60),			//阻塞数量
				Executors.defaultThreadFactory(),  				//创建线程工厂
				new ThreadPoolExecutor.CallerRunsPolicy()		//拒绝策略
				);
		try {
			for (int i = 0; i < 15; i++) {
				executor.execute(()->{
					System.out.println(Thread.currentThread().getName()+"处理过!");
				});
			}
		} finally {
			// TODO: handle finally clause
			executor.shutdown();
		}
	}
}

二、newFixedThreadPool

ExecutorService executorService = Executors.newFixedThreadPool(50);   //指定线程池线程数量

三、newSingleThreadExecutor

ExecutorService executorService = Executors.newSingleThreadExecutor();  //线程池一个线程数

四、newCachedThreadPool

ExecutorService executorService = Executors.newCachedThreadPool();   //线程池自动分配线程
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值