JAVA多线程同步的简单例子(—)

来源:互联网 发布:不一样的天空知乎 编辑:程序博客网 时间:2024/06/06 06:39

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) {
                            }       
                        }
            }           
}                   

原创粉丝点击