首页 技术 正文
技术 2022年11月15日
0 收藏 854 点赞 5,042 浏览 2185 个字

作业:

  有一个水池,水池容量500L,一边为进水口,一边为出水口,要求进水放水不能同时进行,水池一旦满了不能继续注水,一旦空了,不能继续放水,进水速度5L/s,放水速度2L/s。

  这是我学多线程时做的一道练习题,刚开始对wait()方法存在错误理解导致运行时报异常—–java.lang.IllegalMonitorStateException,修复后,在此把错误写法以及最终正确写法都整理出来。

class Water{

static int litre = 500;

boolean flag=true; //false为可以继续加水,true为可以继续放水

}

class OutPour extends Thread{ //出水类

Water w;

OutPour(Water w){

this.w=w;

}

@Override

public void run() { //正确写法

while(true){

synchronized(w){

if(w.flag==true){ //放水,2L/s

try {

Thread.sleep(60);

} catch (InterruptedException e) {

e.printStackTrace();

}

w.litre=w.litre-2;

System.out.println(“放水中,现在还剩”+w.litre+”升水!”);

if(w.litre<=0){ //放空了

w.flag=false;

System.out.println(“——–空了!——–“);

w.notify();

}

}else{

try {

w.wait();//等加满

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

/*

@Override

public void run() { //错误写法

while(true){

if(w.flag==true){ //放水,2L/s

synchronized(w){

try {

Thread.sleep(60);

} catch (InterruptedException e) {

e.printStackTrace();

}

w.litre=w.litre-2;

System.out.println(“放水中,现在还剩”+w.litre+”升水!”);

if(w.litre<=0){ //放空了

w.flag=false;

System.out.println(“——–空了!——–“);

w.notify

}

}

}else{

try {

w.wait();//等加满

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

*/

}

class InPour extends Thread{ //进水类

Water w;

InPour(Water w){

this.w=w;

}

@Override

public void run() { //正确写法

while(true){

synchronized(w){

if(w.flag==false){//加水,5L/s

try {

Thread.sleep(60);

} catch (InterruptedException e) {

e.printStackTrace();

}

w.litre=w.litre+5;

System.out.println(“加水中,现在有”+w.litre+”升水!”);

if(w.litre>=500){//加满了

System.out.println(“——-满了!——-“);

w.flag=true;

w.notify();

}

}else{

try {

w.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

}

/*

@Override

public void run() { //错误写法

while(true){

if(w.flag==false){ //加水,5L/s

synchronized(w){

try {

Thread.sleep(60);

} catch (InterruptedException e) {

e.printStackTrace();

}

w.litre=w.litre+5;

if(w.litre>=500){ //加满了

System.out.println(“——-满了!——-“);

w.flag=true;

w.notifyAll();

}

}

}else{

try {

w.wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

}

*/

}

public class Demo11 {

public static void main(String[] args){

Water w = new Water();

OutPour o = new OutPour(w);

InPour i = new InPour(w);

o.start();

i.start();

}

}

  run方法在业务逻辑上并没有错,报异常java.lang.IllegalMonitorStateException,是因为wait()方法必须要放在同步代码块中才能使用。把else{}语句也圈到synchronized代码块即可。也奉劝,先把笔记看了之后再敲代码,能为调试省不少时间。。。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,941
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,465
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,280
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,094
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,728
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765