标签:str class dem inter led util mamicode read exception
我们用两种方式创建的线程,在使用后都会被销毁,频繁地创建和销毁会造成时间和资源的浪费。线程池是一个能够容纳多个线程的容器,里面的线程可以反复使用。
package pers.zhb.runnable;
public class MyThread implements Runnable {
public void run() {
System.out.println(Thread.currentThread().getName());
}
}
package pers.zhb.runnable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class RunnableDemo {
public static void main(String[] args) throws InterruptedException {
ExecutorService es = Executors.newFixedThreadPool(3);// ?
// Executors:线程池创建工厂类,调用方法返回线程池对象
es.submit(new MyThread());
es.submit(new MyThread());
es.submit(new MyThread());
}
}
标签:str class dem inter led util mamicode read exception
原文地址:https://www.cnblogs.com/zhai1997/p/11370340.html