/**
* 创建线程的方式四:使用线程池
*
* 思路:
* 提前创建好多个线程,放入线程池中,使用时直接获取,使用完放回池中。可以避免频繁
* 创建销毁、实现重复利用
*
* 好处:
* 1. 提高响应速度(减少了创建新线程的时间)
* 2. 降低资源消耗(重复利用线程池中线程,不需要每次都创建)
* 3. 便于线程管理
* corePollSize:核心池的大小
* maximumPoolSize:最大线程数
* keepAliveTime:线程没有任务时最多保持多长时间后会终止
* @author rjn
* @create 04-21 23:24
*/
class NumberThread implements Runnable{
@Override
public void run() {
for (int i = 0;i <= 100;i++){
if (i % 2 == 0){
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}
class NumberThread1 implements Runnable{
@Override
public void run() {
for (int i = 0;i <= 100;i++){
if (i % 2 != 0){
System.out.println(Thread.currentThread().getName() + ":" + i);
}
}
}
}
public class ThreadPool {
public static void main(String[] args) {
//1. 提供指定线程数量的线程池
ExecutorService service = Executors.newFixedThreadPool(10);
ThreadPoolExecutor service1 = (ThreadPoolExecutor) service;
//设置线程池的属性
System.out.println(service.getClass());
service1.setCorePoolSize(15);
// service1.setKeepAliveTime();
//2. 执行指定的线程的操作 需要提供实现Runnable接口或Callable接口实现类的对象
service.execute(new NumberThread());//适合使用于Runnable
service.execute(new NumberThread1());//适合使用于Runnable
// service.submit(Callable callable);//适合使用于Callable
//3. 关闭连接池
service.shutdown();
}
}
class java.util.concurrent.ThreadPoolExecutor
pool-1-thread-1:0
pool-1-thread-1:2
pool-1-thread-1:4
pool-1-thread-1:6
pool-1-thread-1:8
pool-1-thread-1:10
pool-1-thread-1:12
pool-1-thread-1:14
pool-1-thread-1:16
pool-1-thread-1:18
pool-1-thread-1:20
pool-1-thread-1:22
pool-1-thread-1:24
pool-1-thread-1:26
pool-1-thread-1:28
pool-1-thread-1:30
pool-1-thread-1:32
pool-1-thread-1:34
pool-1-thread-1:36
pool-1-thread-1:38
pool-1-thread-1:40
pool-1-thread-1:42
pool-1-thread-1:44
pool-1-thread-1:46
pool-1-thread-2:1
pool-1-thread-1:48
pool-1-thread-1:50
pool-1-thread-1:52
pool-1-thread-1:54
pool-1-thread-1:56
pool-1-thread-1:58
pool-1-thread-1:60
pool-1-thread-1:62
pool-1-thread-1:64
pool-1-thread-1:66
pool-1-thread-1:68
pool-1-thread-1:70
pool-1-thread-1:72
pool-1-thread-1:74
pool-1-thread-1:76
pool-1-thread-1:78
pool-1-thread-1:80
pool-1-thread-1:82
pool-1-thread-1:84
pool-1-thread-1:86
pool-1-thread-1:88
pool-1-thread-1:90
pool-1-thread-1:92
pool-1-thread-1:94
pool-1-thread-1:96
pool-1-thread-1:98
pool-1-thread-1:100
pool-1-thread-2:3
pool-1-thread-2:5
pool-1-thread-2:7
pool-1-thread-2:9
pool-1-thread-2:11
pool-1-thread-2:13
pool-1-thread-2:15
pool-1-thread-2:17
pool-1-thread-2:19
pool-1-thread-2:21
pool-1-thread-2:23
pool-1-thread-2:25
pool-1-thread-2:27
pool-1-thread-2:29
pool-1-thread-2:31
pool-1-thread-2:33
pool-1-thread-2:35
pool-1-thread-2:37
pool-1-thread-2:39
pool-1-thread-2:41
pool-1-thread-2:43
pool-1-thread-2:45
pool-1-thread-2:47
pool-1-thread-2:49
pool-1-thread-2:51
pool-1-thread-2:53
pool-1-thread-2:55
pool-1-thread-2:57
pool-1-thread-2:59
pool-1-thread-2:61
pool-1-thread-2:63
pool-1-thread-2:65
pool-1-thread-2:67
pool-1-thread-2:69
pool-1-thread-2:71
pool-1-thread-2:73
pool-1-thread-2:75
pool-1-thread-2:77
pool-1-thread-2:79
pool-1-thread-2:81
pool-1-thread-2:83
pool-1-thread-2:85
pool-1-thread-2:87
pool-1-thread-2:89
pool-1-thread-2:91
pool-1-thread-2:93
pool-1-thread-2:95
pool-1-thread-2:97
pool-1-thread-2:99