package com.itheima.d8_threadpool;
import java.util.concurrent.*;
/**
* 目标:自定义一个线程池对象,并测试其特性。
*
*/
public class ThreadPoolDemo3 {
public static void main(String[] args) throws Exception {
//1.创建固定线程数据的线程池
ExecutorService pool = Executors.newFixedThreadPool(3);
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
pool.execute(new MyRunnable());
pool.execute(new MyRunnable()); //已经没有多余线程了
}
}
package com.itheima.d8_threadpool;
public class MyRunnable implements Runnable{
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println(Thread.currentThread().getName() + "输出了: HelloWorld ==>" + i);
}
try {
System.out.println(Thread.currentThread().getName() + "本任务与线程绑定了,线程进入了休眠了~~~");
Thread.sleep(10000000);
} catch (Exception e) {
e.printStackTrace();
}
}
}