自定义线程池

一、自定义线程池

1、定义拒绝策略-函数式接口

package ThreadPoolDemo20230911;

// 自定义拒绝策略
@FunctionalInterface
public interface RejectPolicy<T> {
    void reject(MyBlockingQueue<T> blockingQueue, T task);
}

2、定义存放任务的队列

package ThreadPoolDemo20230911;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class MyBlockingQueue<T> {
    // 队列容量
    private int capacity;
    // 任务队列
    private Deque<T> queue = new ArrayDeque<>();
    private ReentrantLock lock = new ReentrantLock();
    private Condition fullCondition = lock.newCondition();
    private Condition emptyCondition = lock.newCondition();

    public MyBlockingQueue(int capacity) {
        this.capacity = capacity;
    }

    // 阻塞加入队列
    public void put(T t) {
        lock.lock();
        try {
            while (queue.size() == capacity) {
                try {
                    System.out.println(Thread.currentThread().getName() + "等待加入队列。。" + t);
                    fullCondition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "加入队列。。" + t);
            queue.addLast(t);
            emptyCondition.signal();
        } finally {
            lock.unlock();
        }
    }

    // 阻塞获取任务
    public T take() {
        lock.lock();
        try {
            while (queue.size() <= 0) {
                try {
                    emptyCondition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            T t = queue.pollFirst();
            fullCondition.signal();
            return t;
        } finally {
            lock.unlock();
        }
    }

    // 超时获取任务
    public T poll(long timeout,TimeUnit unit){
        lock.lock();
        long nanos = unit.toNanos(timeout);
        try {
            while (queue.size() <= 0) {
                try {
                    if (nanos<0){
                        return null;
                    }
                    nanos = emptyCondition.awaitNanos(nanos);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            T t = queue.pollFirst();
            fullCondition.signal();
            return t;
        } finally {
            lock.unlock();
        }
    }

    // 超时加入队列
    public boolean offer(T t, long timeout, TimeUnit unit) {
        lock.lock();
        long nanos = unit.toNanos(timeout);
        try {
            while (queue.size() == capacity) {
                try {
                    if (nanos < 0) {
                        return false;
                    }
                    System.out.println(Thread.currentThread().getName() + "等待加入队列。。" + t);
                    nanos = fullCondition.awaitNanos(nanos);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println(Thread.currentThread().getName() + "加入队列。。" + t);
            queue.addLast(t);
            emptyCondition.signal();
            return true;
        } finally {
            lock.unlock();
        }
    }

    // 从形参加入拒绝策略的任务加入队列方法
    public void tryPut(RejectPolicy<T> rejectPolicy,T task){
        lock.lock();
        try {
            if (queue.size() == capacity){
                rejectPolicy.reject(this,task);
            }else{
                System.out.println("加入任务队列:" + task);
                queue.addLast(task);
                emptyCondition.signal();
            }
        }finally {
            lock.unlock();
        }
    }
}

3、自定义线程池

package ThreadPoolDemo20230911;

import java.util.HashSet;
import java.util.concurrent.TimeUnit;

public class MyThreadPool {
    // 线程池核心线程数
    private int coreSize;
    // 超时时间
    private long timeout;
    // 时间单位
    private TimeUnit timeUnit;
    // 拒绝策略
    private RejectPolicy<Runnable> rejectPolicy;
    // 任务等待队列
    private MyBlockingQueue<Runnable> blockingQueue;
    // 任务集合
    private HashSet<Runnable> workers = new HashSet<>();

    public MyThreadPool(int coreSize, long timeout, TimeUnit timeUnit, int queueCapacity, RejectPolicy<Runnable> rejectPolicy) {
        this.coreSize = coreSize;
        this.timeout = timeout;
        this.timeUnit = timeUnit;
        this.rejectPolicy = rejectPolicy;
        blockingQueue = new MyBlockingQueue<Runnable>(queueCapacity);
    }

    public void execute(Runnable task) {
        synchronized (workers) {
            if (workers.size() >= coreSize) {
//                blockingQueue.put(task);
//                blockingQueue.offer(task,1000,TimeUnit.MILLISECONDS);
                blockingQueue.tryPut(rejectPolicy,task);
            } else {
                Worker worker = new Worker(task);
                System.out.println(Thread.currentThread().getName() + "新增worker:" + worker + ",task:" + task);
                workers.add(worker);
                worker.start();
            }
        }
    }

    // 任务类
    class Worker extends Thread {
        private Runnable task;

        public Worker(Runnable task) {
            this.task = task;
        }

        @Override
        public void run() {
//            while (task != null || (task = blockingQueue.take()) != null) {
            while (task != null || (task = blockingQueue.poll(timeout,timeUnit)) != null) {
                try {
                    System.out.println(Thread.currentThread().getName() + "正在执行" + task);
                    task.run();
                } finally {
                    task = null;
                }
            }
            synchronized (workers) {
                System.out.println(Thread.currentThread().getName() + "worker被移除:" + this.toString());
                workers.remove(this);
            }
        }
    }
}

4、测试

package ThreadPoolDemo20230911;

import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) {
        MyThreadPool threadPool = new MyThreadPool(
                1,
                1000,
                TimeUnit.MILLISECONDS,
                2,
                (queue, task) -> {
//                    queue.put(task);
//                    queue.offer(task,1000,TimeUnit.MILLISECONDS);
                    task.run();
                }
        );
        for (int i = 0; i < 5; i++) {
            int j = i;
            threadPool.execute(() -> {
                System.out.println(Thread.currentThread().getName() + "执行任务:" + j);
                try {
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
    }
}

二、工作线程,让各自的任务都有自己的处理线程

package WorkerThreadDemo20230911;

import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

// 工作线程,让各自的任务都有自己的处理线程
public class WorkerThread {
    static final List<String> menu = Arrays.asList("m1","m2","m3","m4","m5");
    static final Random RANDOM = new Random();

    public static void main(String[] args) {
        // 接收订单线程
        ExecutorService waitExecutorService = Executors.newFixedThreadPool(1);
        // 处理订单线程
        ExecutorService cookExecutorService = Executors.newFixedThreadPool(1);
        waitExecutorService.execute(()->{
            System.out.println("order...");
            Future<String> future = cookExecutorService.submit(() -> {
                System.out.println("cooking...");
                return cooking();
            });
            try {
                System.out.println("serving "+future.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
        waitExecutorService.execute(()->{
            System.out.println("order1...");
            Future<String> future = cookExecutorService.submit(() -> {
                System.out.println("cooking1...");
                return cooking();
            });
            try {
                System.out.println("serving1 "+future.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        });
    }
    public static String cooking(){
        return menu.get(RANDOM.nextInt(menu.size()));
    }
}

三、任务类,将一个大任务分成多个小任务,最后将结果汇总

package ForkJoinDemo20230911;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;

public class ForkJoinDemo {
    public static void main(String[] args) {
        ForkJoinPool pool = new ForkJoinPool(4);
        System.out.println(pool.invoke(new MyTask(1, 5)));
    }
}

// 任务类,将一个大任务分成多个小任务,最后将结果汇总
class MyTask extends RecursiveTask<Integer> {

    private int begin;
    private int end;

    public MyTask(int begin, int end) {
        this.begin = begin;
        this.end = end;
    }

    @Override
    protected Integer compute() {
        if (begin == end) {
            return begin;
        }
        if ((end - begin) == 1) {
            return begin + end;
        }
        int mid = (begin + end) / 2;
        MyTask taskLeft = new MyTask(begin, mid);
        MyTask taskRight = new MyTask(mid + 1, end);
        // 将小任务加入到一起
        taskLeft.fork();
        taskRight.fork();
        System.out.println(taskLeft);
        System.out.println(taskRight);
        // 等待所有小任务完成后进行汇总
        return taskLeft.join() + taskRight.join();
    }

    @Override
    public String toString() {
        return "MyTask{" +
                "begin=" + begin +
                ", end=" + end +
                '}';
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值