Java线程2-2 固定大小的线程池FixedThreadPool

来源:互联网 发布:淘宝买家诈骗商家手段 编辑:程序博客网 时间:2024/06/06 05:44
public class ThreadPoolTest {public static void main(String[] args) throws InterruptedException {//定义一个可执行3个线程的线程池ExecutorService threadPool = Executors.newFixedThreadPool(3);//循环6次,但是该线程池只会执行3个线程for (int i = 0; i < 6; i++) {MyThread t = new MyThread();//执行线程threadPool.execute(t);Thread.sleep(500);}}}class MyThread implements Runnable{@Overridepublic void run() {System.out.println("当前线程:"+Thread.currentThread().getName());}}
执行结果:
当前线程:pool-1-thread-1当前线程:pool-1-thread-2当前线程:pool-1-thread-3当前线程:pool-1-thread-1当前线程:pool-1-thread-2当前线程:pool-1-thread-3


0 0
原创粉丝点击