创建线程池
import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class Demo { public static void main(String[] args) { ExecutorService es= Executors.newFixedThreadPool(4); Runnable runnable=new Runnable() { private int ticket=100; @Override public void run() { while(true){ if(ticket<=0){ break; } System.out.println(Thread.currentThread().getName()+"卖了第"+ticket+"张票"); ticket--; } } }; for(int i=0;i<4;i++){ es.submit(runnable); } es.shutdown(); } }