多线程运行正常但没有达到自己那种想无限循环下去的效果,不信可以自己拷过去试下,几秒钟就停住了
public class TestThread {
public static void main(String[] args) {
Parent p1 = new Parent("爸爸",1000);
Parent p2 = new Parent("妈妈",1000);
Child c1 = new Child("孩子1",300);
Child c2 = new Child("孩子2",500);
Thread t1 = new Thread(p1);
Thread t2 = new Thread(p2);
Thread t3 = new Thread(c1);
Thread t4 = new Thread(c2);
t1.start();t2.start();t3.start();t4.start();
}
}
class BankCard {
  int sum=0;
  public synchronized void save(String s,int count) throws Exception{
  while(sum > 3000) {
    System.out.println(s+"你等等,已经有足够存款了");
    this.wait();
    }
    this.sum += count;
        notifyAll();
        System.out.println(s+"村了"+count+"块钱,孩子们,快来取用吧");
    } 
    public synchronized void disposit(String s,int count) throws Exception { 
        while(sum < count) {
    System.out.println(s+"你等等,钱不够了");
    this.wait();
    }
    this.sum -= count;
    notifyAll();
        System.out.println(s+"取出了"+count+"块钱");
       
    } 
}   
class Parent implements Runnable {
String s; int count; BankCard b = new BankCard();
Parent(String s,int count) {
this.s = s;
this.count = count;
}
public void run() {
while(true) {
    try{
b.save(s,count);
Thread.sleep(2000);
}
catch(Exception e) {
}
}
}
}
class Child implements Runnable {
String s; int count; BankCard b = new BankCard();
Child(String s,int count) {
this.s = s;
this.count = count;
}
public void run() {
while(true) {
try {
b.disposit(s,count);
Thread.sleep(2000);
}
catch(Exception e) {
}
}
}
}
      

解决方案 »

  1.   

    不是循环还有线程可以一直这样运行下去并打印到命令行上吗,怎么我这马上就停不动了
    执行结果:
    爸爸村了1000块钱,孩子们,快来取用吧
    孩子1你等等,钱不够了
    妈妈村了1000块钱,孩子们,快来取用吧
    孩子2你等等,钱不够了
    爸爸村了1000块钱,孩子们,快来取用吧
    妈妈村了1000块钱,孩子们,快来取用吧
    爸爸村了1000块钱,孩子们,快来取用吧
    妈妈村了1000块钱,孩子们,快来取用吧
    爸爸村了1000块钱,孩子们,快来取用吧
    妈妈村了1000块钱,孩子们,快来取用吧
    爸爸你等等,已经有足够存款了
    妈妈你等等,已经有足够存款了
    下面就不动了,寻高人讲解
    thank you
      

  2.   

    这是必然的,你开了4个线程,每个线程里你都  
    BankCard b = new BankCard();
    这4个线程不是共享一份数据,而是各自有着自己的数据。所以他们都是对自己的数据进行处理,并没有数据共享。这样父母亲存满自己的那份钱就wait了,孩子取完自己的钱就wait了
    我讲的不清楚,贴一下我改的代码.public class TestThread {
                public static void main(String[] args) {
                 BankCard b = new BankCard();//4个线程共享数据,将这个对象传给4个线程,而不是在线程中new
                            Parent p1 = new Parent("爸爸",1000,b);
                            Parent p2 = new Parent("妈妈",1000,b);
                            Child c1 = new Child("孩子1",300,b);
                            Child c2 = new Child("孩子2",500,b);
                            Thread t1 = new Thread(p1);
                            Thread t2 = new Thread(p2);
                            Thread t3 = new Thread(c1);
                            Thread t4 = new Thread(c2);
                            t1.start();t2.start();t3.start();t4.start();
                }    
    }    
    class BankCard {
                 int sum=0;
                 public synchronized void save(String s,int count) throws Exception{
                             while(sum > 3000) {
                                              System.out.println(s+"你等等,已经有足够存款了");
                                              this.wait();
                              }
                              this.sum += count;
                                notifyAll();
                                System.out.println(s+"村了"+count+"块钱,孩子们,快来取用吧");
                  }             
                  public synchronized void disposit(String s,int count) throws Exception {                     
                                while(sum < count) {
                              System.out.println(s+"你等等,钱不够了");
                              this.wait();
                              }
                              this.sum -= count;
                              notifyAll();
                                System.out.println(s+"取出了"+count+"块钱");
                                            
                  }         
    }                              
    class Parent implements Runnable {
                String s;        int count; BankCard b;
                Parent(String s,int count,BankCard b) {
                        this.s = s;
                        this.count = count;
                        this.b = b;
                }        
                public void run() {
                            while(true) {
                                try{
                                        b.save(s,count);
                                        Thread.sleep(2000);
                                    }
                                    catch(Exception e) {
                                }        
                            }        
                }
    }                    
    class Child implements Runnable {
                String s;        int count; BankCard b;
                Child(String s,int count,BankCard b) {
                        this.s = s;
                        this.count = count;
                        this.b = b;
                }        
                public void run() {
                            while(true) {
                                    try {
                                        b.disposit(s,count);
                                        Thread.sleep(2000);
                                    }
                                    catch(Exception e) {
                                }        
                            }
                }            
    }