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

线程池

  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);
            }
        }
    }
    

你可能感兴趣的:(【Java】【Thread多线程】线程池)