首页 > 编程语言 > 详细

线程池

时间:2019-12-29 16:04:30      阅读:92      评论:0      收藏:0      [点我收藏+]
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class ExecutorUtil
{
    //自定义线程池:
     private static final int CPU_COUNT = Runtime.getRuntime() 
       .availableProcessors();// 获取当前CPU最大的可执行线程数
     private static final int CORE_POOL_SIZE = 2;//CPU_COUNT * 2;// 线程池中执行的线程数
     private static final int MAXIMUM_POOL_SIZE = CORE_POOL_SIZE  + 1;// 线程池可以缓存的线程最大数
     private static final int KEEP_ALIVE = 1; // 缓存线程的存活时间数
     private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(
       128); // 线程等待的队列
     private static final ThreadFactory sThreadFactory = new ThreadFactory()
     { // 线程工厂,用于创建线程
      private final AtomicInteger mCount = new AtomicInteger(1);// 线程安全的计数器
      public Thread newThread(Runnable r)
      {
       return new Thread(r, "自定义线程" + mCount.getAndIncrement());
      }
     };
     public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(
       CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.MINUTES,
       sPoolWorkQueue, sThreadFactory);

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

public class Test
{
    public static void main(String[] args)
    {
        //1、创建一个同一时间只能执行一个任务的线程
//        ExecutorService service = 
//                Executors.newSingleThreadExecutor();
        
//        MyRunnable r1 = new MyRunnable("任务1");
//        MyRunnable r2 = new MyRunnable("任务2");
//        MyRunnable r3 = new MyRunnable("任务3");
//        MyRunnable r4 = new MyRunnable("任务4");
//        MyRunnable r5 = new MyRunnable("任务5");
//        MyRunnable r6 = new MyRunnable("任务6");
//        service.execute(r1);
//        service.execute(r2);
//        service.execute(r3);
//        service.execute(r4);
//        service.execute(r5);
//        service.execute(r6);
//        service.shutdown();  //提交任务完关闭程序    
        
        
        //2、创建一个同一时间能执行指定个任务的线程
//        ExecutorService service = 
//                Executors.newFixedThreadPool(4);
        
//        MyRunnable r1 = new MyRunnable("任务1");
//        MyRunnable r2 = new MyRunnable("任务2");
//        MyRunnable r3 = new MyRunnable("任务3");
//        MyRunnable r4 = new MyRunnable("任务4");
//        MyRunnable r5 = new MyRunnable("任务5");
//        MyRunnable r6 = new MyRunnable("任务6");
        
//        service.execute(r1);
//        service.execute(r2);
//        service.execute(r3);
//        service.execute(r4);
//        service.execute(r5);
//        service.execute(r6);
//        service.shutdown();
        
        //可以缓存线程的线程池  (60秒,在60秒内开启新线程会去复用已开启的线程)
//        ExecutorService service = 
//                Executors.newCachedThreadPool();
//        MyRunnable r1 = new MyRunnable("任务1");
//        MyRunnable r2 = new MyRunnable("任务2");
//        MyRunnable r3 = new MyRunnable("任务3");
//        MyRunnable r4 = new MyRunnable("任务4");
//        MyRunnable r5 = new MyRunnable("任务5");
//        MyRunnable r6 = new MyRunnable("任务6");
//        service.execute(r1);
//        service.execute(r2);
//        service.execute(r3);
//        service.execute(r4);
//        service.execute(r5);
//        service.execute(r6);
//        service.shutdown();  //提交任务完关闭程序    
        
        //4、
        MyRunnable r1 = new MyRunnable("任务1");
        MyRunnable r2 = new MyRunnable("任务2");
        MyRunnable r3 = new MyRunnable("任务3");
        MyRunnable r4 = new MyRunnable("任务4");
        MyRunnable r5 = new MyRunnable("任务5");
        MyRunnable r6 = new MyRunnable("任务6");
        
        ExecutorUtil.THREAD_POOL_EXECUTOR.execute(r1);//提交任务
        ExecutorUtil.THREAD_POOL_EXECUTOR.execute(r2);
        ExecutorUtil.THREAD_POOL_EXECUTOR.execute(r3);
        ExecutorUtil.THREAD_POOL_EXECUTOR.execute(r4);
        ExecutorUtil.THREAD_POOL_EXECUTOR.execute(r5);
        ExecutorUtil.THREAD_POOL_EXECUTOR.execute(r6);
        
        
    }
}

class MyRunnable implements Runnable
{
    private String info;

    public MyRunnable(String info)
    {
        this.info = info;
    }
    
    @Override
    public void run()
    {
        System.out.println(Thread.currentThread().getName() + ":" + info );
        try
        {
            Thread.sleep(2000);
        } catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}

线程池

原文:https://www.cnblogs.com/xyyou/p/12115139.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!