java学习之线程池的实现

package com.gh.threadpoor;

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

/**
 * 使用线程池
 * @author ganhang
 *
 */
public class ThreadPoorDemo {
    public static void main(String[] args) {
        //创建一个单线程的线程池 
        ExecutorService es=Executors.newSingleThreadExecutor();
        //创建一个固定大小的线程池(2个)
        ExecutorService es1=Executors.newFixedThreadPool(2);
        //创建一个可缓存的线程池,保存线程60s再回收
        ExecutorService es2=Executors.newCachedThreadPool();
        //创建一个无限制大小的线程池,需要给定一个起始大小(最小的大小),但不会回收
        ExecutorService es3=Executors.newScheduledThreadPool(2);
        Mythread m1=new Mythread(); 
        Mythread m2=new Mythread(); 
        es.execute(m1); 
        es.execute(m2);
        
    }
}
class Mythread implements Runnable{
    @Override
    public void run() {
        for(int i=0;i<10;i++){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"--"+i);
        }
    }
    
}
原文地址:https://www.cnblogs.com/ganhang-acm/p/5154362.html