了解参数
四大线程池
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);
}
}
延时一秒执行,每三秒执行一次
延时一秒执行,每三秒执行一次
延时一秒执行,每三秒执行一次