线程池的简单使用

package thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * TODO
 * 线程池的简单使用
 * @author fw
 * @date 2019/9/2 14:58
 */
public class ThreadPool {

    public static void main(String[] args){
        ExecutorService pool = Executors.newFixedThreadPool(2);
        /**
         * 有返回结果
         */
        pool.submit(new MyRunnable());
        pool.submit(new MyRunnable());

        /**
         * 无返回结果
         */
        pool.execute(new MyRunnable());
        pool.execute(new MyRunnable());

        pool.shutdown();
    }

}

class MyRunnable implements Runnable{

    @Override
    public void run() {
        for (int i = 0; i < 5; i++){
            System.out.println(Thread.currentThread().getName() + ":" + i);
        }
    }
}

  运行结果:

  

原文地址:https://www.cnblogs.com/fanerwei222/p/11446715.html