线程卖票问题(消费和供给问题)

在银行柜台存钱,两人轮流存1000元,各存三次

public class Count {
int M=0;
String name ="mmm";
}
import java.util.concurrent.locks.ReentrantLock;
public class Men implements Runnable{
Count count;
public Men(Count count) {
this.count=count;
}
public Men() {
}
@Override
public void run() {
while(count.M<6000)
{
inCount();
}
}
public synchronized void inCount()
{
this.notify();
count.M = count.M + 1000;
System.out.println(Thread.currentThread().getName() + "存了1000元,现
有" + count.M + "元");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Test {
public static void main(String[] args) {
Count count=new Count();
Men man=new Men(count);
Thread thread1=new Thread(man,"第一人");
Thread thread2=new Thread(man,"第二人");
thread1.start();
thread2.start();
}
}

你可能感兴趣的:(python,java,开发语言)