• 【Java__线程池】ThreadPoolExecutor


    ThreadPoolExecutor

    ThreadPoolExecutorDemo

    package sys.test;
    
    import java.util.concurrent.ArrayBlockingQueue;
    import java.util.concurrent.ThreadPoolExecutor;
    import java.util.concurrent.TimeUnit;
    
    public class ThreadPoolExecutorDemo {
        private static final int CORE_POOL_SIZE = 5;
        private static final int MAX_POOL_SIZE = 10;
        private static final int QUEUE_CAPACITY = 100;
        private static final Long KEEP_ALIVE_TIME = 1L;
    
        public static void main(String[] args) {
    
            ThreadPoolExecutor executor = new ThreadPoolExecutor(
                    CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE_TIME, TimeUnit.SECONDS
                    , new ArrayBlockingQueue<Runnable>(QUEUE_CAPACITY)
                    , new ThreadPoolExecutor.CallerRunsPolicy());
            for (int i = 0; i < 10; i++) {
                Runnable worker = new MyRunnable("==" + i);
                executor.execute(worker);
            }
            executor.shutdown();
            while (!executor.isTerminated()) {
    
            }
            System.out.println("Finished all threads");
        }
    }
    
    

    MyRunnable

    package sys.test;
    
    import java.util.Date;
    
    public class MyRunnable implements Runnable {
        private String commond;
    
        public MyRunnable(String commond) {
            this.commond = commond;
        }
    
        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "start time - " + new Date());
            processCommond();
            System.out.println(Thread.currentThread().getName() + "end time - " + new Date());
        }
    
        private void processCommond() {
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public String toString() {
            return this.commond;
        }
    }
    
    
  • 相关阅读:
    【转】很全的TeeChart for .NET中文教程
    对比两个flash金融图表Stock Chart vs AnyStock
    FusionCharts实例大全
    [译]金融图表AnyStock的9个使用技巧
    看懂SqlServer查询计划
    怎样修改MySQL的默认编码
    VS2003+自带水晶报表的打包部署(CS方式)
    ADO.NET 连接池理解
    临时表 & 表变量
    Eclipse 快捷键介绍
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/13047353.html
Copyright © 2020-2023  润新知