public class TestPool {
public static void main(String[] args) {
//创建服务,创建线程池
//newFixedThreadPool 参数为:线程池大小
ExecutorService service = Executors.newFixedThreadPool(10);
//执行
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
service.execute(new MyThread());
//关闭链接
service.shutdown();
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
ExecutorService :真正的线程池接口。
void execute(Runnable command):执行任务\命令,没有返回值,一般用来执行Runnable
<T>Future<T>submit(Callable<T> task): 执行任务,有返回值,一般又来执行Callable
void shutdown():关闭线程池
Exectors:工具类、线程池的工厂类,用于创建并返回不同类型的线程池