zoukankan      html  css  js  c++  java
  • 线程池

    package com.xinboedu.www.test;
    
    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
    {//https://blog.csdn.net/jubaoquan/article/details/79198780
        //自定义线程池:
         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);
    
    }
        
    package com.xinboedu.www.test;
    
    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);
    //        try
    //        {
    //            Thread.sleep(3000);
    //        } catch (InterruptedException e)
    //        {
    //            // TODO Auto-generated catch block
    //            e.printStackTrace();
    //        }
    //        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);
            
            ExecutorService service = (ExecutorService) ExecutorUtil.THREAD_POOL_EXECUTOR;
            service.shutdown();//借助向上转型关掉线程池
        }
    }
    
    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();
            }
        }
    }
  • 相关阅读:
    [转]简单理解php的socket编程
    github 生成token的方法
    win7升级powershell【转】
    PHP的反射机制 【转】
    jquery幻灯片插件slick演示
    织梦生成的时候“你指定的文件名有问题,无法创建文件”解决方案【转】
    dedecms下的tplcache模板缓存[转]
    dede织梦data目录正确迁移及引起的问题解决方法【转】
    C# 判断是否是在设计模式下有效的方法
    C# 操作计算机用户权限
  • 原文地址:https://www.cnblogs.com/xyyou/p/12115139.html
Copyright © 2011-2022 走看看