Java 手动实现带拒绝策略的ThreadPool BlockingQueue

@Slf4j(topic = "c.Test")
public class Test {
    static AtomicStampedReference<String> s = new AtomicStampedReference<>("A", 0);
    @SneakyThrows
    public static void main(String[] args) {
        ThreadPool threadPool = new ThreadPool(1, 1, TimeUnit.SECONDS, 3, (queue, task) -> {
        	//reject policy
        	//here is throw Exception
            log.debug("already full.");
            throw new RuntimeException();
        });
        for (int i = 0; i < 5; i++) {
            int j = i;
            threadPool.execute(() -> {
                try {
                    Thread.sleep(1_000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                log.debug("execute log -> {}", j);
            });
        }
    }
}

@Slf4j(topic = "c.ThreadPool")
class ThreadPool {
    private BlockingQueue<Runnable> taskQueue;

    private int coreSize;
    private HashSet<Worker> workers = new HashSet<Worker>();

    private long timeout;
    private TimeUnit timeUnit;

    private RejectPolicy<Runnable> rejectPolicy;

    public ThreadPool(int coreSize, long timeout, TimeUnit timeUnit, int queueSize,
                      RejectPolicy<Runnable> rejectPolicy) {
        this.coreSize = coreSize;
        this.timeout = timeout;
        this.timeUnit = timeUnit;
        this.taskQueue = new BlockingQueue<>(queueSize);
        this.rejectPolicy = rejectPolicy;
    }

    public void execute(Runnable task) {
        synchronized (workers) {
            if (workers.size() < coreSize) {
                log.debug("new worker: {}", task);
                Worker worker = new Worker(task);
                workers.add(worker);
                worker.start();
            } else {
                log.debug("add taskQueue: {}", task);
                taskQueue.add(task, rejectPolicy);
            }
        }
    }

    class Worker extends Thread{
        private Runnable task;

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

        @Override
        public void run() {
            while (task != null || (task = taskQueue.take(1, timeUnit)) != null) {
                try {
                    log.debug("run task: {}", task);
                    task.run();
                } finally {
                    task = null;
                }
            }

            synchronized (workers) {
                log.debug("remove worker: {}", this);
                workers.remove(this);
            }
        }
    }
}

@Slf4j(topic = "c.BlockingQueue")
class BlockingQueue<T> {
    private Deque<T> deque;
    private ReentrantLock lock = new ReentrantLock();
    private Condition fullWaitSet = lock.newCondition();
    private Condition emptyWaitSet = lock.newCondition();
    private int size;

    public BlockingQueue(int size) {
        this.size = size;
        deque = new ArrayDeque<>();
    }

    public T take() {
        try {
            lock.lock();
            while (deque.isEmpty()) {
                emptyWaitSet.await();
            }
            T t = deque.removeFirst();
            fullWaitSet.signal();
            return t;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    public T take(long timeout, TimeUnit unit) {
        try {
            lock.lock();
            long nanos = unit.toNanos(timeout);
            while (deque.isEmpty()) {
                if (nanos <= 0) {
                    return null;
                }
                nanos = emptyWaitSet.awaitNanos(nanos);
            }
            T t = deque.removeFirst();
            fullWaitSet.signal();
            return t;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    public boolean add(T t) {
        try {
            lock.lock();
            while (deque.size() == size) {
                fullWaitSet.await();
            }
            deque.addLast(t);
            emptyWaitSet.signal();
            return true;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    public boolean add(T t, long timeout, TimeUnit unit) {
        try {
            lock.lock();
            long nanos = unit.toNanos(timeout);
            while (deque.size() == size) {
                if (nanos <= 0) {
                    return false;
                }
                nanos = fullWaitSet.awaitNanos(nanos);
            }
            deque.addLast(t);
            emptyWaitSet.signal();
            return true;
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            lock.unlock();
        }
    }

    public boolean add(T t, RejectPolicy<T> rejectPolicy) {
        try {
            lock.lock();
            if (deque.size() == size) {
                log.debug("full.");
                rejectPolicy.reject(this, t);
            } else {
                deque.addLast(t);
                emptyWaitSet.signal();
            }
            return true;
        } finally {
            lock.unlock();
        }
    }
}

@FunctionalInterface
interface RejectPolicy<T> {
    void reject(BlockingQueue<T> queue, T task);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值