線程池的基本使用

public class ThreadPoolExecutorDemo {
    private static final ExecutorService pool = new ThreadPoolExecutor(4, 4, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(
            10000), new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            System.out.println("Thread pool is FULL.Failed to submit the task to save call trace data.");
        }
    });
    public static void test() {
        for (int i = 0; i < 30000; i++) {
            pool.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName() + "線程被執行");
                }
            });
        }
    }
    public static void main(String[] args) {
        ThreadPoolExecutorDemo.test();
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章