您的位置:首页 > 编程语言 > Java开发

java多线程和synchronized应用实例

2013-02-15 16:19 621 查看
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();

      

 

 }

 

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: