这是一段类似于生产者消费者的程序,好像出现死锁了,求高手帮忙啊!!~~
class A extends Thread{
private Withdraw withdraw;
public A(Withdraw w){
withdraw=w;
start();
}
public void run(){
while(true){
while(withdraw.remain==200)
synchronized(this){
try{
wait();
}catch(InterruptedException e){
throw new RuntimeException(e);
}
}
withdraw.remain=withdraw.remain-50;
System.out.println( withdraw.remain);
withdraw.remain=200;
}
}
}
class B extends Thread{
private Withdraw withdraw;
private A aa;
public B(Withdraw w,A a){
withdraw=w;
aa=a;
}
public void run(){
while(true){
if(withdraw.remain==200)
{ withdraw.remain=withdraw.remain+100;
System.out.println(withdraw.remain);
synchronized(aa){
aa.notify();//通知Consumer进程
}
}
try{
sleep(100);
}catch(InterruptedException e){
throw new RuntimeException(e);
}
}
}
}
public class Withdraw{
public int remain=200;
public static void main(String[]args){
Withdraw withdraw=new Withdraw();
A aa=new A(withdraw);
B b=new B(withdraw,aa);
}
}
class A extends Thread{
private Withdraw withdraw;
public A(Withdraw w){
withdraw=w;
start();
}
public void run(){
while(true){
while(withdraw.remain==200)
synchronized(this){
try{
wait();
}catch(InterruptedException e){
throw new RuntimeException(e);
}
}
withdraw.remain=withdraw.remain-50;
System.out.println( withdraw.remain);
withdraw.remain=200;
}
}
}
class B extends Thread{
private Withdraw withdraw;
private A aa;
public B(Withdraw w,A a){
withdraw=w;
aa=a;
}
public void run(){
while(true){
if(withdraw.remain==200)
{ withdraw.remain=withdraw.remain+100;
System.out.println(withdraw.remain);
synchronized(aa){
aa.notify();//通知Consumer进程
}
}
try{
sleep(100);
}catch(InterruptedException e){
throw new RuntimeException(e);
}
}
}
}
public class Withdraw{
public int remain=200;
public static void main(String[]args){
Withdraw withdraw=new Withdraw();
A aa=new A(withdraw);
B b=new B(withdraw,aa);
}
}
假设一个银行的ATM机,它可以允许用户存款也可以取款。现在一个账户上有存款200元,用户A和用户B都拥有在这个账户上存款和取款的权利。用户A将存入100元,而用户B将取出50元,那么最后账户的存款应是250元。实际操作过程如下:
(1) 先进行A的存款操作:
得到账户的存款数额200,耗时2s。
将账户数额增加100,耗时忽略不计
将新生成的账户结果300返回到ATM机的服务器上,耗时2s
(2) 再进行B的取款操作:
得到增加后账户存款数额300,耗时2s。
判断取款额是否小于账户余额,若是,则将账户数额减少50,否则抛出异常信息,耗时忽略不计。
将新生成的账户结果250返回到ATM机的服务器上,耗时2s。
请根据以上要求,将A的操作和B的操作分别用线程来表示
* Discription.
* User: Hellfire
* Date: 2006-12-7
* Time: 11:18:19
*/
public class Test {
public static void main(String[] args) {
Account account = new Account(200); new Thread(new PersonA(account)).start();
new Thread(new PersonB(account)).start();
}
}class Account {
private int value; public Account(int v) {
this.value = v;
} public void save(int v) {
value += v;
} public void take(int v) {
if (value < v) {
throw new RuntimeException("No enough money!");
}
value -= v;
} public int getValue() {
return this.value;
}
}class PersonA implements Runnable {
private Account account; public PersonA(Account account) {
this.account = account;
} public void run() { synchronized (account) {
try {
Thread.sleep(200);
System.out.println("account remain: " + account.getValue());
account.save(100);
System.out.println("A save into account: 100");
Thread.sleep(300);
System.out.println("account remain: " + account.getValue());
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
}class PersonB implements Runnable {
private Account account; public PersonB(Account account) {
this.account = account;
} public void run() { synchronized (account) {
try {
Thread.sleep(200);
System.out.println("account remain: " + account.getValue());
account.take(50);
System.out.println("B take from account: 50");
Thread.sleep(300);
System.out.println("account remain: " + account.getValue());
} catch (InterruptedException e) {
e.printStackTrace();
} }
}
}