最简单的线程池的实现
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadPoolDemo { public static void main(String[] args) { ExecutorService es = Executors.newFixedThreadPool(2); //创建了两个线程对象 es.submit(new Runable1()); //es.submit(需要执行的某个线程中的run方法中的内容) es.submit(new Runable1()); es.submit(new Runable1()); } } //执行结果 始终就两个线程 因为线程池就只创建了2个 //pool-1-thread-2 //pool-1-thread-2 //pool-1-thread-1
public class Runable1 implements Runnable { @Override public void run() { System.out.println(Thread.currentThread().getName()); } }