基本生产者消费者模型:
代码示例:
数据模型:
* Created by IntelliJ IDEA.
*
* @Author: ZhangDong
* @Date: 2019/9/9 16:00
*/
public class Message {
private String tittle;
private String content;
public String getTittle() {
return tittle;
}
public void setTittle(String tittle) {
this.tittle = tittle;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
生产者模型:
* Created by IntelliJ IDEA.
*
* @Author: ZhangDong
* @Date: 2019/9/9 16:00
*/
public class Producer implements Runnable{
private Message msg;
public Producer(Message msg) {
this.msg = msg;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0){
this.msg.setTittle("1.");
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.msg.setContent("第一种数据模型");
}else {
this.msg.setContent("2.");
this.msg.setContent("第二种数据模型");
}
}
}
}
消费者模型:
* Created by IntelliJ IDEA.
*
* @Author: ZhangDong
* @Date: 2019/9/9 16:00
*/
public class Consumer implements Runnable{
private Message msg;
public Consumer(Message msg) {
this.msg = msg;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(this.msg.getTittle()+" "+this.msg.getContent());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
主程序:
* Created by IntelliJ IDEA.
*
* @Author: ZhangDong
* @Date: 2019/9/9 16:00
*/
public class Main {
public static void main(String[] args) {
Message msg = new Message();
//启动生产者模型
new Thread(new Producer(msg)).start();
//启动消费者模型
new Thread(new Consumer(msg)).start();
}
}
执行结果:
控制台打印结果令人大跌眼镜,说好的生产一个模型取走一个模型呢?连生产出来的模型都乱了,1号模型对应这2号模型的内容,居然还有null。此时我发现了两个主要问题:
问题一: 数据不同步;
问题二:重复生产和重复取出问题;
问题解决
如果要解决问题,首要解决的就是数据同步的处理问题,如果想要解决数据同步最简单的方法就是使用synchronized关键字定义同步代码块或同步方法,于是这个时候对于同步问题的处理就可以直接在Message类中完成。
解决同步问题:
在进行同步处理的时候肯定需要有一个同步处理的对象,那么此时肯定要将同步操作交由Message来处理。在数据模型Message中修改生产和消费方法,并加上synchronized关键字同步方法。
解决同步问题后的代码示例:
数据模型:
* Created by IntelliJ IDEA.
*
* @Author: ZhangDong
* @Date: 2019/9/9 16:10
*/
public class Message {
private String tittle;
private String content;
public synchronized void set(String tittle, String content) {
this.tittle = tittle;
try {
//模拟生产过程 10毫秒
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.content = content;
}
public synchronized String get() {
try {
//模拟消费过程 10毫秒
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
return this.tittle + " - " + this.content;
}
}
生产者模型:
* Created by IntelliJ IDEA.
*
* @Author: ZhangDong
* @Date: 2019/9/9 16:10
*/
public class Producer implements Runnable {
private Message msg;
public Producer(Message msg) {
this.msg = msg;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
this.msg.set("1.", "第一种数据模型");
} else {
this.msg.set("2.", "第二种数据模型");
}
}
}
}
消费者模型:
* Created by IntelliJ IDEA.
*
* @Author: ZhangDong
* @Date: 2019/9/9 16:10
*/
public class Consumer implements Runnable {
private Message msg;
public Consumer(Message msg) {
this.msg = msg;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(this.msg.get());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
主程序:
* Created by IntelliJ IDEA.
*
* @Author: ZhangDong
* @Date: 2019/9/9 16:10
*/
public class Main {
public static void main(String[] args) {
Message msg = new Message();
//启动生产者模型
new Thread(new Producer(msg)).start();
//启动消费者模型
new Thread(new Consumer(msg)).start();
}
}
执行结果:
根据控制台打印输出可以观察到,刚刚发现的同步问题已经解决了,生产和消费的模型信息都是正确的 1号模型对应第一种数据模型,2号对应第二种数据模型,完全正确,但重复生产重复消费问题依然存在。
接下来我来解决这个重复生产重复消费的问题,典型思路就是,设置一个标记,好比十字路口的红绿灯,绿灯亮时生产者进行生产,消费者等待,红灯亮时消费者进行消费,而生产者停止生产等待消费者消费完成信号灯变为绿色时再进行生产。
终极版本代码示例:
数据模型:
* Created by IntelliJ IDEA.
*
* @Author: ZhangDong
* @Date: 2019/9/9 16:14
*/
public class Message {
private String tittle;
private String content;
/** flag属性控制生产与消费
* true:允许生产,不允许消费 false:允许消费,不允许生产
* 默认可以生产
*/
private boolean flag = true;
public synchronized void set(String tittle,String content){
if (!this.flag){
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
this.tittle=tittle;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.content=content;
//标记为已生产
this.flag=false;
//唤醒等待线程
super.notify();
}
public synchronized String get() {
if (this.flag){
try {
super.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
return this.tittle+" - "+this.content;
}finally {
//继续生产
this.flag=true;
//唤醒等待线程
super.notify();
}
}
}
生产者模型:
* Created by IntelliJ IDEA.
*
* @Author: ZhangDong
* @Date: 2019/9/9 16:14
*/
public class Producer implements Runnable{
private Message msg;
public Producer(Message msg) {
this.msg = msg;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
if (i % 2 == 0){
this.msg.set("1.","第一种数据模型");
}else {
this.msg.set("2.","第二种数据模型");
}
}
}
}
消费者模型:
* Created by IntelliJ IDEA.
*
* @Author: ZhangDong
* @Date: 2019/9/9 16:14
*/
public class Consumer implements Runnable{
private Message msg;
public Consumer(Message msg) {
this.msg = msg;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
System.out.println(this.msg.get());
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
主程序:
* Created by IntelliJ IDEA.
*
* @Author: ZhangDong
* @Date: 2019/9/9 16:14
*/
public class Main {
public static void main(String[] args) {
Message msg = new Message();
//启动生产者模型
new Thread(new Producer(msg)).start();
//启动消费者模型
new Thread(new Consumer(msg)).start();
}
}
执行结果:
至此,由生产者消费者模型引出的线程同步问题已经解决,希望能给看到此文章的你一些思路以及解决方法。