崇之他和她

导航

day 016 Thread

线程池

  • 线程不需要新建销毁,降低资源消耗,实现资源重用

  • 管理线程大小等

  • ExecutorService Executors

  • package pool;
    
    import java.util.concurrent.Callable;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    import java.util.concurrent.FutureTask;
    
    /**
     * @ Author     :wwwzhqwww
     * @ Date       :Created in 16:35 2021/1/20
     * @ Description:线程池
     * @ Modified By:
     * @Version: $version$
     */
    public class TestPool {
        public static void main(String[] args) {
            ExecutorService executorService = Executors.newFixedThreadPool(10);
            for (int i = 0; i < 10; i++) {
                executorService.execute(new MyThread());
            }
    
            executorService.shutdownNow();
            new MyThread3().start();
            FutureTask<Integer> futureTask =new FutureTask<Integer>(new MyThread2());
            new Thread(futureTask).start();
        }
    }
    class MyThread implements Runnable{
    
        @Override
        public void run() {
                System.out.println(Thread.currentThread().getName()+"线程:");
        }
    }
    class MyThread2 implements Callable<Integer>{
    
        @Override
        public Integer call() throws Exception {
            System.out.println("i am callable");
            return 100;
        }
    }
    class MyThread3 extends Thread{
        @Override
        public void run() {
            System.out.println("im thread");
        }
    }
    

posted on 2021-01-20 18:18  崇之他和她  阅读(24)  评论(0编辑  收藏  举报