请老师检查一下

请老师检查一下

/**
 * 自定义线程池类
 */
package threadpool;

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

public class PauseThread extends ThreadPoolExecutor {

    Boolean isPause = false;
    ReentrantLock lock = new ReentrantLock();
    Condition condition = lock.newCondition();
    public PauseThread(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
    }

    public PauseThread(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    public PauseThread(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
    }

    public PauseThread(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

    @Override
    protected void beforeExecute(Thread t, Runnable r) {
        super.beforeExecute(t, r);
        lock.lock();
        try {
            while (isPause) {
                condition.await();
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }finally {
            lock.unlock();
        }

    }

    public void pause(){
        lock.lock();
        try{
            isPause = true;
        }finally {
            lock.unlock();
        }
    }
    public void resume(){
        lock.lock();
        try{
            isPause = false;
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }
}
/**
 * 测试类
 */
package threadpool;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Test {
    public static void main(String[] args) throws InterruptedException {
       Runnable runnable = new Runnable(){
            @Override
            public void run() {
                System.out.println("正在放水");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    System.out.println("======interrupt======");
                }
            }
        };

        PauseThread pauseThread = new PauseThread(10, 20, 10l, TimeUnit.SECONDS, new LinkedBlockingQueue<>());

        System.out.println("往水池放入1000吨水");
        for (int i = 0; i < 1000; i++) {
            pauseThread.execute(runnable);
        }

        Thread.sleep(200);
        System.out.println("暂停放水中...");
        pauseThread.pause();
        System.out.println("中午啦,水池暂停放水,还剩" + pauseThread.getQueue().size() + "吨水");

        Thread.sleep(200);
        System.out.println("下午啦,开始放水啦");
        pauseThread.resume();

        Thread.sleep(200);
        System.out.println("下午5点啦,开始停啦,将在10秒后强制停水");
        pauseThread.shutdown();
        if(pauseThread.isShutdown()){
            System.out.println("开始停水正常");
        }

        Boolean bool = pauseThread.awaitTermination(100, TimeUnit.MILLISECONDS);
        if(!bool){
            List<Runnable> runnableList =  pauseThread.shutdownNow();
            System.out.println("启动强制停水,还剩" + runnableList.size() + "吨水");
        }else{
            System.out.println("输出停水正常,水池没水了");
        }

    }
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小蓝 2023-08-04 15:24:14

同学你好,同学的代码符合题目要求,逻辑清晰,书写规范,做的很棒。

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师