1 生产者与消费者基本程序模型
在多线程的开发过程之中最著名的案例就是生产者与消费者操作,该操作的主要流程如下:
(1)生产者负责信息内容的的生产;
(2)每当生产者生产完成一项完整的信息之后消费者要从这里面取走信息;
(3)如果生产者没有生产则消费者要等待它生产完成,如果消费者还没有对信息进行消费,则生产者应该等待消费处理完成后继续生产。
可以将生产者和消费者定义为两个独立的线程对象,但是对于现在生产的数据,可以使用如下的组成:
(1)数据一:title=lks,content=code;
(2)数据二:title=hhy,content=lover;
既然生产者与消费者是两个独立的线程,那么这两个独立的线程之间就需要有一个数据的保存的集中点,那么可以单独定义一个Message类实现数据的保存。
范例:程序基本结构
package cn.victor.demo;
public class MutipleDemo {
public static void main(String[] args) {
Message msg = new Message();
new Thread(new Producer(msg)).start();
new Thread(new Consumer(msg)).start();
}
}
class Message{
private String title;
private String content;
public Message() {}
public void setTitle(String title) {
this.title = title;
}
public void setContent(String content) {
this.content = content;
}
public String getTitle() {
return this.title;
}
public String getContent() {
return this.content;
}
}
class Producer implements Runnable{
private Message msg;
public Producer() {}
public Producer(Message msg) {
this.msg = msg;
}
@Override
public void run() {
for(int i = 0; i <100; i++) {
if( i % 2 == 0) {
this.msg.setTitle("lks");
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.msg.setContent("code");
}else {
this.msg.setTitle("hhy");
try {
Thread.sleep(2);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.msg.setContent("lover");
}
}
}
}
class Consumer implements Runnable{
private Message msg;
public Consumer() {}
public Consumer(Message msg) {
this.msg = msg;
}
@Override
public void run() {
for(int i=0; i < 100; i++) {
System.out.println(this.msg.getTitle() + " - " + this.msg.getContent());
}
}
}
通过整个代码的执行会发现此时有两个主要问题:
(1)问题一:数据不同步了;
(2)问题二:生产一个取走一个,但是发现有重复生产和重复取出问题。
2 解决生产者-消费者同步问题
如果要解决问题,首先要解决的就是数据同步的处理问题,如果要想解决数据同步最简单的做法就是使用synchronized关键字定义同步代码块和同步方法,于是这个时候对于同步的处理就可以直接在Message类中完成。
范例:解决同步操作
package cn.victor.demo;
public class MutipleDemo {
public static void main(String[] args) {
Message msg = new Message();
new Thread(new Producer(msg)).start();
new Thread(new Consumer(msg)).start();
}
}
class Message{
private String title;
private String content;
public Message() {}
public synchronized void set(String title, String content) {
this.title = title;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.content = content;
}
public synchronized String get() {
return this.title + " - " + this.content;
}
}
class Producer implements Runnable{
private Message msg;
public Producer() {}
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("lks", "code");
}else {
this.msg.set("hhy", "lover");
}
}
}
}
class Consumer implements Runnable{
private Message msg;
public Consumer() {}
public Consumer(Message msg) {
this.msg = msg;
}
@Override
public void run() {
for(int i=0; i < 100; i++) {
System.out.println(this.msg.get());
}
}
}
在进行同步处理的时候肯定需要有一个同步的处理对象,那么此时肯定要将同步操作交由Message类处理是最合适的。这个时候数据已经可以正常保持一致,但是对于重复操作依然存在。
3 利用Object类解决重复操作(线程等待与唤醒)
如果现在要想解决生产者与消费者的问题,那么最好的解决方案就是使用等待与唤醒机制。而对于等待与唤醒的操作机制主要依靠的是Object类中提供的方法处理的:
(1)等待机制:
|——死等:public final void wait() throws InterruptedException;
|——设置等待时间:public final void wait(long timeout) throws InterruptedException;
|——设置等待时间:public final void wait(long timeout, int nanos) throws InterruptedException
(2)唤醒第一个等待线程:public final void notify();
(3)唤醒全部等待线程:public final void notifyAll();
如果此时有若干个等待线程的话,那么notify()表示的是唤醒第一个等待的,而其他的线程继续等待,而notifyAll()表示会唤醒所有等待线程,那个线程优先级高就有可能先执行。
对于当前问题主要的解决应该通过Message类完成处理。
范例:修改Message类
package cn.victor.demo;
public class MutipleDemo {
public static void main(String[] args) {
Message msg = new Message();
new Thread(new Producer(msg)).start();
new Thread(new Consumer(msg)).start();
}
}
class Message{
private String title;
private String content;
private boolean flag = true; //true, 可以生产,消费等待
//false, 开始消费,生产等待
public Message() {}
public synchronized void set(String title, String content) {
if(!this.flag) {
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.title = title;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.content = content;
this.flag = false;
super.notify();
}
public synchronized String get() {
if(this.flag) {
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
return this.title + " - " + this.content;
}finally {
this.flag = true;
super.notify();
}
}
}
class Producer implements Runnable{
private Message msg;
public Producer() {}
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("lks", "code");
}else {
this.msg.set("hhy", "lover");
}
}
}
}
class Consumer implements Runnable{
private Message msg;
public Consumer() {}
public Consumer(Message msg) {
this.msg = msg;
}
@Override
public void run() {
for(int i=0; i < 100; i++) {
System.out.println(this.msg.get());
}
}
}
这种处理形式就是在进行多线程开发过程之中最原始的处理方案,整个的等待、同步、唤醒机制都有开发者自行完成。