线程池的使用方法
线程池
主要是为了提升性能,线程重复利用。每个线程都创建十分消耗性能。线程池就像共享单车。
package Thread;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class TestThreadPool {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool (10);//第一步
service.execute (new MyThread ());//第二部
service.execute (new MyThread ());
service.execute (new MyThread ());
service.execute (new MyThread ());
service.submit (new MyThread ());//有返回值,但是我也不知道怎么用?
service.shutdown ();//关闭线程池
}
}
class MyThread implements Runnable{
@Override
public void run() {
System.out.println (Thread.currentThread ().getName ());
}
}