50,526
社区成员
发帖
与我相关
我的任务
分享
public class TakeAndPour {
public static void main(String args[]) {
WaterTaking wt = new WaterTaking();
WaterPouring wp = new WaterPouring();
Thread t1 = new Thread(wt, "Xiaohao");
Thread t2 = new Thread(wp, "doubi");
t1.start();
t2.start();
}
}
class Tank {
private static int count = 10; // 缸里原有的水
//private static int max = 5;
private static boolean flag = true; //防止同时取水、 倒入水
public void takeWater() {
synchronized (this) {
while (!flag) {
try {
System.out
.println("The tank is being used now");
this.wait();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
flag = false; //同一时刻只允许一个对水缸操作
try {
Thread.sleep(800);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count--;
System.out.println(flag+" "+Thread.currentThread().getName()
+ " take out some water" + " now water remain " + count);
flag = true;
this.notifyAll(); //操作完了就把flag置为true,并唤醒线程,这里使用notify()一样。
}
}
public void pourWater() {
synchronized (this) {
flag = false;
while (!flag) {
try {
System.out
.println("the tank is being used");
this.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
Thread.sleep(800);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
count++;
System.out.println(flag+" "+Thread.currentThread().getName()
+ " pour in some water" + " now water remain " + count);
flag = true;
this.notifyAll();
}
}
}
class WaterTaking implements Runnable {
Tank t = new Tank();
public void run() {
while(true){
t.takeWater();
}
}
}
class WaterPouring implements Runnable {
Tank t = new Tank();
public void run() {
while(true){
t.pourWater();
}
}
}
package test_my;
public class TakeAndPour {
public static void main(String args[]) {
WaterTaking wt = new WaterTaking();
WaterPouring wp = new WaterPouring();
Thread t1 = new Thread(wt, "Xiaohao");
Thread t2 = new Thread(wp, "doubi");
t1.start();
t2.start();
}
}
class Tank {
private static int count = 10; // 缸里原有的水
//private static int max = 5;
//防止同时取水、 倒入水
public static boolean flag = false;
public void takeWater() {
synchronized (this) {
//同一时刻只允许一个对水缸操作
if(count>0){
count--;
System.out.println(flag+" "+Thread.currentThread().getName()
+ " take out some water" + " now water remain " + count);
}
this.notifyAll(); //操作完了就把flag置为true,并唤醒线程,这里使用notify()一样。
}
}
public void pourWater() {
synchronized (this) {
count++;
System.out.println(flag+" "+Thread.currentThread().getName()
+ " pour in some water" + " now water remain " + count);
this.notifyAll();
}
}
}
class WaterTaking implements Runnable {
Tank t = new Tank();
int m=10;
public void run() {
while(m-->0){
t.takeWater();
}
}
}
class WaterPouring implements Runnable {
Tank t = new Tank();
int m=10;
public void run() {
while(m-->0){
t.pourWater();
}
}
}
[/quote]
你这个全局变量有问题package test_my;
public class TakeAndPour {
public static void main(String args[]) {
WaterTaking wt = new WaterTaking();
WaterPouring wp = new WaterPouring();
Thread t1 = new Thread(wt, "Xiaohao");
Thread t2 = new Thread(wp, "doubi");
t1.start();
t2.start();
}
}
class Tank {
private static int count = 10; // 缸里原有的水
//private static int max = 5;
//防止同时取水、 倒入水
public static boolean flag = false;
public void takeWater() {
synchronized (this) {
//同一时刻只允许一个对水缸操作
if(count>0){
count--;
System.out.println(flag+" "+Thread.currentThread().getName()
+ " take out some water" + " now water remain " + count);
}
this.notifyAll(); //操作完了就把flag置为true,并唤醒线程,这里使用notify()一样。
}
}
public void pourWater() {
synchronized (this) {
count++;
System.out.println(flag+" "+Thread.currentThread().getName()
+ " pour in some water" + " now water remain " + count);
this.notifyAll();
}
}
}
class WaterTaking implements Runnable {
Tank t = new Tank();
int m=10;
public void run() {
while(m-->0){
t.takeWater();
}
}
}
class WaterPouring implements Runnable {
Tank t = new Tank();
int m=10;
public void run() {
while(m-->0){
t.pourWater();
}
}
}