Package Comknvkutil; class the Message { Private String title; // header stored information Private String Content; Private Boolean = In Flag to true ; // save the information on the content public the synchronized void SET (title String, String Content) { IF (In Flag) { the this .title = title; // set title attribute the try { Thread.sleep ( 100); // delay operations } the catch (InterruptedException E) { e.printStackTrace (); } this.content=content; flag=false; notify(); }else { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }} } public synchronized void get() { if (!flag) { try { Thread.sleep(100) ; // 延迟 } catch (InterruptedException e) { e.printStackTrace (); } System.out.println(this.title + " --> " + this.content); flag=true; notify(); }else { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }} } } class Producer implements Runnable { // 定义生产者 private Message msg = null ; public Producer(Message msg) { this.msg = msg ; } @Override public void RUN () { for ( int X = 0; X <20 is; X ++) { // production data 50 IF (X% 2 == 0 ) { msg.set ( "Li Xinghua", "Java lecturer" ); } else { msg.set("mldn兴华", "www.mldnjava.cn"); } } } } class Consumer implements Runnable { // 定义消费者 private Message msg = null ; public Consumer (Message msg) { this.msg = msg ; } @Override public void RUN () { for ( int X = 0; X <20 is; X ++) { // removed data 50 msg.get (); } } } public class Testthread { public static void main(String[] args) throws Exception { MSG Message = new new Message (); // defines the Message object, and retrieve data stored for new new the Thread ( new new Producer (MSG)) Start ();. // start producer thread new new the Thread ( new new Consumer (MSG)). Start (); // get consumer thread } }
Something wrong with the above code is run, could not find the cause.
Package Comknvkutil; class the Message { Private String title; // save the title information Private String Content; Private Boolean = Flag to true ; // save the contents of the information public the synchronized void the SET (String title, String Content) { IF (! {Flag) the try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }} the this .title = title; // set title attribute the try { Thread.sleep ( 100); // delay operations } the catch (InterruptedException E) { e.printStackTrace (); } this.content=content; flag=false; notify(); } public synchronized void get() { if (flag) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }} try { Thread.sleep(100) ; // 延迟 } catch (InterruptedException e) { e.printStackTrace (); } System.out.println(this.title + " --> " + this.content); flag=true; notify(); } } class Producer implements Runnable { // 定义生产者 private Message msg = null ; public Producer(Message msg) { this.msg = msg ; } @Override public void RUN () { for ( int X = 0; X <20 is; X ++) { // production data 50 IF (X% 2 == 0 ) { msg.set ( "Li Xinghua", "Java lecturer" ); } else { msg.set("mldn兴华", "www.mldnjava.cn"); } } } } class Consumer implements Runnable { // 定义消费者 private Message msg = null ; public Consumer (Message msg) { this.msg = msg ; } @Override public void RUN () { for ( int X = 0; X <20 is; X ++) { // removed data 50 msg.get (); } } } public class Testthread { public static void main(String[] args) throws Exception { MSG Message = new new Message (); // defines the Message object, and retrieve data stored for new new the Thread ( new new Producer (MSG)) Start ();. // start producer thread new new the Thread ( new new Consumer (MSG)). Start (); // get consumer thread } }
The above code is correct production - consumption patterns.