Wu Yuxiong - born naturally develop common java class library study notes: Thread in Action - producers and consumers

class Info {     // definition information based 
    Private String name = "LiXingHua";      // definition of the name attribute 
    Private String content = "lecturer the JAVA";         // define the content attribute 
    public  void the setName (String name) {
         the this .name = name; 
    } 
    public  void the setContent (String Content) {
         the this .content = Content; 
    } 
    public String getName () {
         return  the this .name; 
    } 
    public String getContent () {
         return  the this  .content;
    }
}; 
Class Producer the implements Runnable {     // through multi-threaded Runnable 
    Private Info info = null ;         // save Info cited 
    public Producer (Info info) {
         the this .info = info; 
    } 
    public  void RUN () {
         Boolean In Flag = to false ;     // define flag 
        for ( int I = 0; I <50; I ++ ) {
             IF (In flag) {
                 the this .info.setName ( "LiXingHua");     // set the name of 
                the try {
                    Thread.sleep(90) ;
                }catch(InterruptedException e){
                    e.printStackTrace() ;
                }
                this.info.setContent("JAVA讲师") ;    // 设置内容
                flag = false ;
            }else{
                this.info.setName("mldn") ;    // 设置名称
                try{
                    Thread.sleep(90) ;
                }catch(InterruptedException e){
                    e.printStackTrace() ;
                }
                this.info.setContent("www.mldnjava.cn") ;    // 设置内容
                flag = true ;
            }
        }
    }
};
class Consumer implements Runnable{
    private Info info = null ;
    public Consumer(Info info){
        this.info = info ;
    }
    public void run(){
        for(int i=0;i<50;i++){
            try{
                Thread.sleep(90) ;
            }catch(InterruptedException e){
                e.printStackTrace() ;
            }
            System.out.println(this.info.getName() + 
                " --> " + this.info.getContent()) ;
        }
    }
};
public class ThreadCaseDemo01{
    public static void main(String args[]){
        Info info = new Info();    // 实例化Info对象
        Producer pro = new Producer(info) ;    // 生产者
        Consumer con = new new  Consumer(info) ;    // consumers 
        new new the Thread (Pro) .start ();
         new new the Thread (CON) .start (); 
    } 
};
class Info {     // definition information based 
    Private String name = "LiXingHua";      // definition of the name attribute 
    Private String content = "lecturer the JAVA";         // define the content attribute 
    public  the synchronized  void SET (String name, String content) {
         the this .setName (name);     // set the name of 
        the try { 
            the Thread.sleep ( 300 ); 
        } the catch (InterruptedException E) { 
            e.printStackTrace (); 
        } 
        the this .setContent (content);     // set contents 
    }
    public synchronized void get(){
        try{
            Thread.sleep(300) ;
        }catch(InterruptedException e){
            e.printStackTrace() ;
        }
        System.out.println(this.getName() + 
            " --> " + this.getContent()) ;
    }
    public void setName(String name){
        this.name = name ;
    }
    public void setContent(String content){
        this.content = content ;
    }
    public String getName () {
         return  the this .name; 
    } 
    public String getContent () {
         return  the this .content; 
    } 
}; 
class Producer the implements Runnable {     // through multi-threaded Runnable 
    Private Info info = null ;         // save Info referenced 
    public Producer (info info) {
         the this .info = info; 
    } 
    public  void RUN () {
         Boolean In Flag = to false ;     //Flag defined 
        for ( int I = 0; I <50; I ++ ) {
             IF (In Flag) {
                 the this .info.set ( "LiXingHua", "JAVA lecturer");     // Sets the name 
                In Flag = to false ; 
            } the else {
                 the this .info.set ( "MLDN", "www.mldnjava.cn");     // Sets the name 
                In Flag = to true ; 
            } 
        } 
    } 
}; 
class Consumer the implements the Runnable {
     Private info info = null ;
    public Consumer(Info info){
        this.info = info ;
    }
    public void run(){
        for(int i=0;i<50;i++){
            this.info.get() ;
        }
    }
};
public class ThreadCaseDemo02{
    public static void main(String args[]){
        Info info = new Info();    // 实例化Info对象
        Producer pro = new Producer(info) ;    // 生产者
        Consumer con = new Consumer(info) ;    // 消费者
        new Thread(pro).start() ;
        new Thread(con).start() ;
    }
};
class Info {     // definition information based 
    Private String name = "LiXingHua";      // definition of the name attribute 
    Private String content = "lecturer the JAVA";         // define the content attribute 
    Private  Boolean In Flag = to false ;     // set flag 
    public  the synchronized  void SET (String name, String Content) {
         IF (! {In Flag)
             the try {
                 Super .wait (); 
            } the catch (InterruptedException E) { 
                e.printStackTrace (); 
            }
        }
         the this .SetName (name);     // Set the name of 
        the try { 
            the Thread.sleep ( 300 ); 
        } the catch (InterruptedException E) { 
            e.printStackTrace (); 
        } 
        the this .setContent (Content);     // set content 
        In Flag = to false ;     / / change flag represents removed 
        Super .notify (); 
    } 
    public  the synchronized  void GET () {
         IF (In flag) {
             the try {
                 Super .wait (); 
            } the catch(InterruptedException e){
                e.printStackTrace() ;
            }
        }
        try{
            Thread.sleep(300) ;
        }catch(InterruptedException e){
            e.printStackTrace() ;
        }
        System.out.println(this.getName() + 
            " --> " + this.getContent()) ;
        flag  = true ;    // 改变标志位,表示可以生产
        super.notify() ;
    }
    public void setName(String name){
        this.name = name ;
    }
    public void setContent(String content){
        this.content = content ;
    }
    public String getName(){
        return this.name ;
    }
    public String getContent(){
        return this.content ;
    }
};
class Producer implements Runnable{    // 通过Runnable实现多线程
    private Info info = null ;        // 保存Info引用
    public Producer(Info info){
        this= .info info; 
    } 
    public  void RUN () {
         Boolean In Flag = to false ;     // define flag 
        for ( int I = 0; I <50; I ++ ) {
             IF (In Flag) {
                 the this .info.set ( "LiXingHua" , "JAVA lecturer");     // Sets the name 
                In Flag = to false ; 
            } the else {
                 the this .info.set ( "MLDN", "www.mldnjava.cn");     // Sets the name 
                In Flag = to true ; 
            } 
        } 
    } 
}; 
class Consumer implements Runnable{
    private Info info = null ;
    public Consumer(Info info){
        this.info = info ;
    }
    public void run(){
        for(int i=0;i<50;i++){
            this.info.get() ;
        }
    }
};
public class ThreadCaseDemo03{
    public static void main(String args[]){
        Info info = new Info();    // 实例化Info对象 
        Producer Pro = new new Producer (info);     // Producer 
        Consumer CON = new new Consumer (info);     // consumers 
        new new the Thread (Pro) .start ();
         new new the Thread (CON) .start (); 
    } 
} ;

 

Guess you like

Origin www.cnblogs.com/tszr/p/12152854.html