package com.neo.util; class Message { private String title ; // 保存信息的标题 private String content ; private Boolean flag=true;// 保存信息的内容 public synchronized void set(String title,String content) { if (flag) { this.title=title ; // 设置title属性 try { Thread.sleep(100) ; // 延迟操作 } 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; x++) { // 生产50次数据 if (x % 2 == 0) { msg.set("李兴华", "Java讲师"); } 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; x++) { // 取走50次数据 msg.get(); } } } public class Testthread { public static void main(String[] args) throws Exception { Message msg = new Message() ; // 定义Message对象,用于保存和取出数据 new Thread(new Producer(msg)).start() ; // 启动生产者线程 new Thread(new Consumer(msg)).start() ; // 取得消费者线程 } }
以上的代码运行时有点问题,暂时找不到原因。
package com.neo.util; class Message { private String title ; // 保存信息的标题 private String content ; private Boolean flag=true;// 保存信息的内容 public synchronized void set(String title,String content) { if (!flag) { try { wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); }} this.title=title ; // 设置title属性 try { Thread.sleep(100) ; // 延迟操作 } 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; x++) { // 生产50次数据 if (x % 2 == 0) { msg.set("李兴华", "Java讲师"); } 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; x++) { // 取走50次数据 msg.get(); } } } public class Testthread { public static void main(String[] args) throws Exception { Message msg = new Message() ; // 定义Message对象,用于保存和取出数据 new Thread(new Producer(msg)).start() ; // 启动生产者线程 new Thread(new Consumer(msg)).start() ; // 取得消费者线程 } }
以上代码是正确的生产-消费模式。