java多线程和synchronized应用实例

来源:互联网 发布:z参数矩阵的求法? 编辑:程序博客网 时间:2024/04/29 19:01

class Info{

 private String str1="祁煜";

 private String str2="学生 ";

 private boolean flag = false;

    public synchronized  voidset(String str1,String str2){

            if(!flag){

                  try{

                        super.wait();

                       

                    

                     }catch(Exceptione){

                    

                        e.printStackTrace();

                    

                     }

                     }

                    

                     this.setStr1(str1);

                 try{

                   Thread.sleep(300);

                

                 }catch(Exception e){

                    e.printStackTrace();

                

                 }

             

               

 

           this.setStr2(str2);

                

                 flag=false;

                 super.notify();

        }

        

                     

               public synchronized void get(){

                  if(flag){

                     try{

                        super.wait();

                    

                    

                     }catch(Exceptione){

                    

                        e.printStackTrace();

                    

                     }

                     }

                    

                     try{

                    

                        Thread.sleep(300);

                    

                     }catch(Exceptione){

                       e.printStackTrace();

                    

                     }

                     System.out.println(this.getStr1()+"--->"+this.getStr2());

                     flag=true;

                     super.notify();

                    

                    

               

               

               }

        

        

        public void setStr1(String str1){

           this.str1=str1;

        

        }

        

        public void setStr2(String str2){

          this.str2=str2;

        

        }

  public String getStr1(){

  

     return this.str1;

  

   }

  public String getStr2(){

  

    return this.str2;

  

   }

 

}

 

 

class Producer implements Runnable{

 private Info info = null;

 public Producer(Info info){

    this.info=info;

 

 

  }

 

 public  void run(){

 

    boolean flag= false;

    for(int i=0;i<50;i++){

          if(flag){

            this.info.set("祁煜","学生");

               flag=false;

               

         

          }else{

            this.info.set("森森","池院");

               flag=true;

         

          }

        

        

        }

   

 

 

  }

 

 

}

 

class consume implements Runnable{

  private Info info = null;

  public consume(Info info){

  this.info=info;

  

  

   }

  

  public void   run(){

     for(int i=0;i<50;i++){

          this.info.get();

        

         }

  

   }

  

 

 

}

 

public class aynthronized0{

 public static void main(String args[]){

 

   Info info =new Info();

       Producerpro= new Producer(info);

       consumecon=new consume(info);

       newThread(pro).start();

       newThread(con).start();

      

 

 }

 

}

原创粉丝点击