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() + "吨水");
}
}
}
22
收起
正在回答 回答被采纳积分+1
2回答
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() + "吨水");
}
}
}
java工程师2020版
- 参与学习 人
- 提交作业 9410 份
- 解答问题 16556 个
综合就业常年第一,编程排行常年霸榜,无需脱产即可学习,北上广深月薪过万 无论你是未就业的学生还是想转行的在职人员,不需要基础,只要你有梦想,想高薪
了解课程





恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星