【Java】【Thread多线程】线程池

2017-04-08  本文已影响10人  JerichoPH

线程池

  1. 基本应用
    public class Demo {
        public static void main(String[] args) throws IOException, InterruptedException {
            ExecutorService pool = Executors.newFixedThreadPool(2);
            pool.submit(new MyRunnable());
            pool.submit(new MyRunnable());
            
            pool.shutdown(); // 关闭线程池
        }
    }
    
    class MyRunnable implements Runnable {
        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "....." + i);
            }
        }
    }
    
上一篇下一篇

猜你喜欢

热点阅读