3-13 自由编程

3-13 自由编程

怎么显示一堆暂停放水中

​package threadpool;

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

public class WaterThread extends ThreadPoolExecutor {

private final ReentrantLock lock = new ReentrantLock();
private boolean isStoped;
private Condition unStoped = lock.newCondition();

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

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

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

public WaterThread(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 (isStoped){
unStoped.await();
System.out.println("暂停放水中");
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}

private void stop(){
lock.lock();
try {
isStoped = true;
}finally {
lock.unlock();
}
}

private void work(){
lock.lock();
try{
isStoped = false;
unStoped.signalAll();
} finally {
lock.unlock();
}
}

public static void main(String[] args) throws InterruptedException {
WaterThread waterThread = new WaterThread(1, 1, 20l,
TimeUnit.SECONDS, new LinkedBlockingDeque<>());
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println("开始停水正常");
}
System.out.println("正在放水");
}
};
System.out.println("上班啦,水池放入1000吨水;");
for(int i = 0; i < 1000; i++){
waterThread.execute(runnable);
}
Thread.sleep(200);
waterThread.stop();
System.out.print("中午啦,回家吃饭啦,水池被暂停啦");
System.out.println("水池还剩" + waterThread.getQueue().size() + "吨水");
Thread.sleep(200);
System.out.println("下午啦,开始放水啦");
waterThread.work();
Thread.sleep(200);
waterThread.shutdown();
if(waterThread.isShutdown()){
System.out.println("下午5点啦 开始停水!将在10s后强制停水");
}
if(waterThread.awaitTermination(100l, TimeUnit.MILLISECONDS)){
System.out.println("停水正常,水池也没有水了");
}else{
List<Runnable> list= waterThread.shutdownNow();
System.out.println("启动强制停水");
System.out.println("水池还剩" + list.size() + "吨水");
}
}
}


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

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

2回答
好帮手慕阿园 2021-04-24 10:56:45

同学你好,代码完成的不错,很棒呐,这里可以通过改变线程池大小以及线程池中允许的最大线程数来控制输出的语句
http://img1.sycdn.imooc.com//climg/608388a009c1f06904540061.jpg

如:

http://img1.sycdn.imooc.com//climg/608388d809d4bcd007870031.jpg

祝学习愉快~

  • 提问者 dh1211 #1

    怎么显示一堆暂停放水中?

    2021-04-24 18:32:28
  • 好帮手慕阿园 回复 提问者 dh1211 #2

    同学你好,这里可以改变线程池的大小,并在beforeExecute()方法中先调用输出语句再调用await()方法,另外可以将正在放水的输出语句在在run()方法中的第一句,并将catch语句中的暂停防水注释掉,如下

    http://img1.sycdn.imooc.com//climg/6083f6950973d7bd04970227.jpg

    http://img1.sycdn.imooc.com//climg/6083f66509629ee709120485.jpg

    http://img1.sycdn.imooc.com//climg/6083f7fb09c0d92205050220.jpg

    祝学习愉快~​

    2021-04-24 19:03:51
dh1211 提问者 2021-04-23 22:08:24
package threadpool;

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

public class WaterThread extends ThreadPoolExecutor {

private final ReentrantLock lock = new ReentrantLock();
private boolean isStoped;
private Condition unStoped = lock.newCondition();

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

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

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

public WaterThread(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 (isStoped){
unStoped.await();
System.out.println("暂停放水中");
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}

private void stop(){
lock.lock();
try {
isStoped = true;
}finally {
lock.unlock();
}
}

private void work(){
lock.lock();
try{
isStoped = false;
unStoped.signalAll();
} finally {
lock.unlock();
}
}

public static void main(String[] args) throws InterruptedException {
WaterThread waterThread = new WaterThread(1, 1, 20l,
TimeUnit.SECONDS, new LinkedBlockingDeque<>());
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println("开始停水正常");
}
System.out.println("正在放水");
}
};
System.out.println("上班啦,水池放入1000吨水;");
for(int i = 0; i < 1000; i++){
waterThread.execute(runnable);
}
Thread.sleep(200);
waterThread.stop();
System.out.print("中午啦,回家吃饭啦,水池被暂停啦");
System.out.println("水池还剩" + waterThread.getQueue().size() + "吨水");
Thread.sleep(200);
System.out.println("下午啦,开始放水啦");
waterThread.work();
Thread.sleep(200);
waterThread.shutdown();
if(waterThread.isShutdown()){
System.out.println("下午5点啦 开始停水!将在10s后强制停水");
}
if(waterThread.awaitTermination(100l, TimeUnit.MILLISECONDS)){
System.out.println("停水正常,水池也没有水了");
}else{
List<Runnable> list= waterThread.shutdownNow();
System.out.println("启动强制停水");
System.out.println("水池还剩" + list.size() + "吨水");
}
}
}


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

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

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

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

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