检查线程池中是否还有线程

import java.util.*;
import java.util.concurrent.*;


public static void main(String[] args) throws Exception {
        ExecutorService exe = Executors.newFixedThreadPool(12);
        for (int i = 1; i <= 5; i++) {
            exe.execute(new Runnable(){
                @Override
                public void run() {
                    try {
                        Thread.sleep(new Random().nextInt(10)*1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("当前线程:" + Thread.currentThread().getName() + ",打印随机数:" + new Random().nextInt(1000));
                }
            });
        }
        exe.shutdown();
        while (true) {
            if (exe.isTerminated()) {
                System.out.println("结束了!");
                break;
            }
            Thread.sleep(1000);
        }
    }
发布了17 篇原创文章 · 获赞 2 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qidaorenzhe/article/details/102667027