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();
            }
        }
    }
  • 相关阅读:
    css常用字体
    多行文本显示省略号,点击展开隐藏
    某个公司采用公用电话传递数据,数据是四位的整数,在传递过程中是加密的, 加密规则如下:每位数字都加上5,然后用除以10的余数代替该数字,再将第一位和第四位交换, 第二位和第三位交换,请编写一个函数,传入原文,输出密文
    编写一个函数,计算任意两个数字之间所能组成的奇数个数,数字必须是个位数。 比如:计算0~3之间能组成的奇数是: 01、03、13、21、23、31
    Redis(一) 数据结构与底层存储 & 事务 & 持久化 & lua
    IO多路复用之Reactor
    IO多路复用之select poll epoll
    五种IO模型
    kafka(五) 流式处理 kafka stream
    kafka(二) 高性能技术分析
  • 原文地址:https://www.cnblogs.com/xyyou/p/12115139.html
Copyright © 2011-2022 走看看