线程---生产者与消费者

来源:互联网 发布:桌面dock软件 编辑:程序博客网 时间:2024/05/22 09:07
public class Demo {public static void main(String[] args) {Info i = new Info();Producer p = new Producer(i);Consumer c = new Consumer(i);new Thread(p).start();new Thread(c).start();}}

public 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.setName("谢永明");
try {Thread.sleep(90);} catch (InterruptedException e) {
e.printStackTrace();}
this.info.setContent("学生");
flag = false;}else{this.info.setName("xym");
try {Thread.sleep(90);} catch (InterruptedException e) 
{e.printStackTrace();}this.info.setContent("www.xym.com");
public 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(100);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(this.info.getName() + "-->" + this.info.getContent());}}}
public class Demo {public static void main(String[] args) {Info i = new Info();Producer p = new Producer(i);Consumer c = new Consumer(i);new Thread(p).start();new Thread(c).start();}}


输出结果:

谢永明-->www.xym.com

xym-->学生
谢永明-->www.xym.com
xym-->学生
谢永明-->www.xym.com
xym-->学生
谢永明-->www.xym.com
xym-->学生
xym-->学生
谢永明-->www.xym.com
xym-->学生
谢永明-->www.xym.com
xym-->学生

谢永明-->www.xym.com


问题解决1 ---假如同步:

public class Info {private String name = "谢永明";//指定默认值private String content = "学生";//指定默认值public synchronized void set(String name,String content){this.setName(name);try {Thread.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}this.setContent(content);}public synchronized void get(){try {Thread.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(this.getName() + "-->" + this.getContent());}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}}

public 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("xym", "www.xym.com");flag = false;}else{this.info.set("谢永明", "学生");flag = true;}}}}

public 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(100);} catch (InterruptedException e) {e.printStackTrace();}this.info.get();}}}

public class Demo {public static void main(String[] args) {Info i = new Info();Producer p = new Producer(i);Consumer c = new Consumer(i);new Thread(p).start();new Thread(c).start();}}

输出结果:

谢永明-->学生
xym-->www.xym.com
xym-->www.xym.com
xym-->www.xym.com
谢永明-->学生
xym-->www.xym.com
谢永明-->学生
xym-->www.xym.com
xym-->www.xym.com
谢永明-->学生


问题解决2---加入等待与唤醒:

public class Info {private String name = "谢永明";//指定默认值private String content = "学生";//指定默认值private boolean flag = false;public synchronized void set(String name,String content){if(!flag){try {super.wait();      //等待消费者取走} catch (InterruptedException e) {e.printStackTrace();}}this.setName(name);try {Thread.sleep(300);} catch (InterruptedException e) {e.printStackTrace();}this.setContent(content);flag = false;super.notify();   //唤醒等待线程}public synchronized void get(){if(flag){try {super.wait();      //等待生产者生产} 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 String getName() {return name;}public void setName(String name) {this.name = name;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}}