101 自定義線程池實現公平鎖以及淘汰策略


package com.taotao.myktthreads.day015;

import java.util.concurrent.*;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author wangjin
 * @title: ZbExecutor
 * @projectName mykt-threads
 * @description:
 * @date 2021/9/28 9:09
 */
public class ZbExecutor {

    static class MyReJectedExecutionHandler1  implements RejectedExecutionHandler {
        @Override
        public void rejectedExecution(Runnable r, java.util.concurrent.ThreadPoolExecutor executor) {
            if (r != null) {
                r.run();
            }
            System.out.println("線程池滿了,拒絕線程任務");
        }
    }
   static ReentrantLock lock = new ReentrantLock(true);
    public  void executor(){

    }
    public  static  ExecutorService newFixedTHreadPool(int corePoolSize,int maximumPoolSize,int queue){
        return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, 0L, TimeUnit.SECONDS,
                //拒絕策略
                new LinkedBlockingQueue<>(queue),new MyReJectedExecutionHandler1()){
           //將要被執行的任務
            @Override
            protected void beforeExecute(Thread t, Runnable r) {
                System.out.println(Thread.currentThread().getName()+"獲取鎖");
                super.beforeExecute(t, r);
            }
          //執行後的任務
            @Override
            protected void afterExecute(Runnable r, Throwable t) {
               System.out.println(Thread.currentThread().getName()+"結束鎖");
                super.afterExecute(r, t);
            }

        };
    }

    public static void main(String[] args) {

         ExecutorService a= Executors.newCachedThreadPool();
        ExecutorService executorService=ZbExecutor.newFixedTHreadPool(10,15,5);
        for (int i = 0; i < 25; i++) {

            try {

                int finalaI=i;
                executorService.execute(new Runnable() {
                    @Override
                    public void run() {
                        lock.lock();
                        try {
                            System.out.println(Thread.currentThread().getName()+"運行鎖");
                        }catch (Exception e){
                            e.printStackTrace();
                        }finally {
                            lock.unlock();
                        }
                    }
                });
               // executorService.shutdown();
            }catch (Exception e){
                e.printStackTrace();
            }finally {

            }

        }
    }
}


pool-2-thread-1獲取鎖
pool-2-thread-4獲取鎖
pool-2-thread-5獲取鎖
pool-2-thread-1運行鎖
pool-2-thread-3獲取鎖
pool-2-thread-2獲取鎖
pool-2-thread-7獲取鎖
pool-2-thread-6獲取鎖
pool-2-thread-8獲取鎖
pool-2-thread-9獲取鎖
pool-2-thread-1結束鎖
pool-2-thread-4運行鎖
pool-2-thread-11獲取鎖
pool-2-thread-5運行鎖
pool-2-thread-1獲取鎖
pool-2-thread-4結束鎖
pool-2-thread-10獲取鎖
pool-2-thread-4獲取鎖
pool-2-thread-3運行鎖
pool-2-thread-5結束鎖
pool-2-thread-2運行鎖
pool-2-thread-3結束鎖
pool-2-thread-7運行鎖
pool-2-thread-2結束鎖
pool-2-thread-5獲取鎖
pool-2-thread-2獲取鎖
pool-2-thread-6運行鎖
pool-2-thread-7結束鎖
pool-2-thread-3獲取鎖
pool-2-thread-6結束鎖
pool-2-thread-8運行鎖
pool-2-thread-8結束鎖
pool-2-thread-9運行鎖
pool-2-thread-9結束鎖
pool-2-thread-11運行鎖
pool-2-thread-11結束鎖
pool-2-thread-1運行鎖
pool-2-thread-1結束鎖
pool-2-thread-10運行鎖
pool-2-thread-10結束鎖
pool-2-thread-4運行鎖
pool-2-thread-4結束鎖
pool-2-thread-5運行鎖
pool-2-thread-5結束鎖
pool-2-thread-2運行鎖
pool-2-thread-2結束鎖
pool-2-thread-3運行鎖
pool-2-thread-3結束鎖

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章