zoukankan      html  css  js  c++  java
  • 【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;
        }
    }
    
    
  • 相关阅读:
    27.全排列与带重复的排列
    ios之自定义UISwitch
    ios之UIAlertView
    ios之UISegmentedcontol
    ios之UISlider
    ios之UITextfield
    ios之UIImageView
    ios之UIButoon
    ios之UILabel
    ios 点餐系统
  • 原文地址:https://www.cnblogs.com/kikyoqiang/p/13047353.html
Copyright © 2011-2022 走看看