package com.git.base.thread.productandconsumer;
/**
* 核心实现:
* 生产者消费者模式:
* 生产一个,消费一个,如果生产未被消费,那么就等待消费后再生产
* 如果消费后,没有下一个生产的,就等待生产后在消费
* <p>Title: DoMain.java</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2016</p>
* <p>Company: Sage</p>
* @author 五虎将
* @date 2016年5月9日下午10:59:51
* @version 1.0
*/
public class DoMain {
public static void main(String[] args) {
Info info = new Info();
ProductInfo product = new ProductInfo(info);
ConsumerInfo consumer = new ConsumerInfo(info);
Thread t1 = new Thread(product);
Thread t2 = new Thread(consumer);
t1.start();
t2.start();
}
}
package com.git.base.thread.productandconsumer;
/**
* 中间信息类-核心类!!!
* 生产者进行生产及消费者进行消费
* 生产者消费者模式的解决点在中间过渡物体类中
* 保证中间类的线程唯一性 从而解决多线程问题
* 什么是多线程问题?其实就是多线程变为单线程再某些点上!
* <p>Title: Info.java</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2016</p>
* <p>Company: Sage</p>
* @author 五虎将
* @date 2016年5月9日下午10:42:08
* @version 1.0
*/
public class Info {
private String name;
private String content;
private boolean flag = false;
/* 1变
public synchronized void set(String name,String content){
this.name = name;
this.content = content;
}
public synchronized void get(){
System.out.println(this.name +" =====" + this.content);
}
*/
public synchronized void set(String name,String content){
if(flag){
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.name = name;
this.content = content;
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
flag = true;
super.notify();
}
public synchronized void get(){
if(!flag){
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(this.name +" =====" + this.content);
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
flag = false;
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;
}
}
package com.git.base.thread.productandconsumer;
public class ConsumerInfo implements Runnable{
private Info info;
public ConsumerInfo(Info info) {
this.info = info;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
info.get();
}
}
}
package com.git.base.thread.productandconsumer;
public class ProductInfo implements Runnable{
private boolean flag = true;
private Info info;
public ProductInfo(Info info) {
this.info = info;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if(flag){
info.set("宋庆虎","喜欢分布式");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
flag = false;
}else{
info.set("龙志建", "喜欢JAVA");
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
flag = true;
}
}
}
}