你想了解的四个线程池,newCachedThreadPool,newFixedThreadPool,newSingleThreadExecutor,newScheduledThreadPool

在这里插入图片描述在这里插入图片描述
了解参数

在这里插入图片描述

四大线程池

在这里插入图片描述
newCachedThreadPool
很根据用户的数量 自动创建线程连接,不清楚用户总量,例如100w(创建1/4左右的连接) 60s之内如果没有连接 借出连接 立刻销毁该连接
一个访问都没有,不创建连接

因为 i 的这块儿作用域不够大!! 直接接收i报错
final int index=i;//这里把i放入了 index的常量池方法里 常量池里的数据被所有方法共享

public class test2 {
	public static void main(String[] args) {
		//很根据用户的数量 自动创建线程连接,不清楚用户总量,例如100w(创建1/4左右的连接) 60s之内如果没有连接 借出连接   立刻销毁该连接
		//一个访问都没有,不创建连接
		ExecutorService ex=Executors.newCachedThreadPool();//缓存的线程池
		for (int i = 0; i < 1000; i++) {
			final int index=i;//这里把i放入了 index的常量池方法里     常量池里的数据被所有方法共享
			ex.execute(new Runnable() { //线程池自动帮忙启动	
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName()+":"+index);//这里作用域太小!拿不到i 所以要			
				}
			});
		}		
//		ex.shutdown();
	}
	

}
pool-1-thread-181:572
pool-1-thread-285:573
pool-1-thread-170:574
pool-1-thread-180:575
pool-1-thread-186:275
pool-1-thread-183:576
pool-1-thread-184:577
pool-1-thread-286:578
pool-1-thread-284:447
pool-1-thread-284:603
pool-1-thread-282:582
pool-1-thread-282:608
pool-1-thread-185:583
pool-1-thread-181:586

newFixedThreadPool

public class Thread2 {
	public static void main(String[] args) {
		ExecutorService 	ex= Executors.newFixedThreadPool(3);//创建固定的线程池
		for (int i = 0; i < 1000; i++) {
			final int index=i;
			  ex.execute(new Runnable() {	
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName()+":"+index);		
				}
			});
			
		}
//		ex.shutdownNow();  会突然打断线程池 不推荐使用
		ex.shutdown();
	}
	
}
pool-1-thread-8:297
pool-1-thread-11:296
pool-1-thread-2:295
pool-1-thread-7:294
pool-1-thread-14:293
pool-1-thread-15:292
pool-1-thread-18:291
pool-1-thread-17:290
pool-1-thread-19:288
pool-1-thread-16:289
pool-1-thread-20:287
pool-1-thread-21:286
pool-1-thread-22:285

newSingleThreadExecutor

单列模式

public class Thread3 {
	public static void main(String[] args) {
		ExecutorService 	ex= Executors.newSingleThreadExecutor();//创建固定的线程池
		for (int i = 0; i < 1000; i++) {
			final int index=i;
			  ex.execute(new Runnable() {	
				@Override
				public void run() {
					System.out.println(Thread.currentThread().getName()+":"+index);		
				}
			});
			
		}
//		ex.shutdownNow();  会突然打断线程池 不推荐使用
		ex.shutdown();//正常运行完以后 关闭
	}
	
}
pool-1-thread-1:333
pool-1-thread-1:334
pool-1-thread-1:335
pool-1-thread-1:336
pool-1-thread-1:337
pool-1-thread-1:338
pool-1-thread-1:339
pool-1-thread-1:340
pool-1-thread-1:341
pool-1-thread-1:342
pool-1-thread-1:343
pool-1-thread-1:344
pool-1-thread-1:345
pool-1-thread-1:346
pool-1-thread-1:347
pool-1-thread-1:348
pool-1-thread-1:349
pool-1-thread-1:350
pool-1-thread-1:351
pool-1-thread-1:352
pool-1-thread-1:353
pool-1-thread-1:354
pool-1-thread-1:355
pool-1-thread-1:356
pool-1-thread-1:357
pool-1-thread-1:358
pool-1-thread-1:359
pool-1-thread-1:360

newScheduledThreadPool

定时任务的管理器

public class Thread4 {
	public static void main(String[] args) {//定时任务的管理器
		ScheduledExecutorService ex=Executors.newScheduledThreadPool(5);
		
//		ex.schedule(new Runnable() {
//			
//			@Override
//			public void run() {
//			System.out.println("3S后执行");
//				
//			}
//		},3,TimeUnit.SECONDS);
//		
////	ex.shutdown();	
		ex.scheduleAtFixedRate(new Runnable() {		
			@Override
			public void run() {
				System.out.println("延时一秒执行,每三秒执行一次");			
			}
		},1,3,TimeUnit.SECONDS);
		
		
		
	} 
	
	

}
延时一秒执行,每三秒执行一次
延时一秒执行,每三秒执行一次
延时一秒执行,每三秒执行一次

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是汤圆丫

怎么 给1分?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值