线程通讯的经典案例--生产者和消费者

本程序的核心结构如下:首先定义两个类:一个生产者线程,另一个消费者线程类,生产者每生产完一个数据之后,消费者要取走这些数据。
现在的数据有两种:
title=小动物,content=草泥马。
title=小金子,content=不是好孩子
范例:代码基本模型
package thread;
class Message{
private String title;
private String content;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
class Productor implements Runnable{
private Message msg;
public Productor(Message msg) {
this.msg=msg;
}
@Override
public void run() {
for(int x=0;x<100;x++) {
if(x%2==0) {
this.msg.setTitle("小动物");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.msg.setContent("草泥马");
}
else {
this.msg.setTitle("小金子");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.msg.setContent("不是好孩子");
}
}
}
}
class Customer implements Runnable{
private Message msg;
public Customer(Message msg) {
this.msg=msg;
}
@Override
public void run() {
for(int x=0;x<100;x++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.msg.getTitle()+","+this.msg.getContent());
}
}
}
public class Threadh {
public static void main(String[] args) {
// TODO Auto-generated method stub
Message msg=new Message();
new Thread(new Productor(msg)).start();
new Thread(new Customer(msg)).start();
}


}
这个时候出现了两个问题:
数据错位:
数据重复取出与重复设置:
解决数据错位问题
要想解决数据错位,使用同步处理即可
package thread;
class Message{
private String title;
private String content;
public synchronized void set(String title,String content) {
this.title=title;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.content=content;
}
public synchronized void get() {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.title+","+this.content);
}
}
class Productor implements Runnable{
private Message msg;
public Productor(Message msg) {
this.msg=msg;
}
@Override
public void run() {
for(int x=0;x<100;x++) {
if(x%2==0) {
this.msg.set("小动物","草泥马");
}
else {
this.msg.set("小金子","不是好孩子");
}
}
}
}
class Customer implements Runnable{
private Message msg;
public Customer(Message msg) {
this.msg=msg;
}
@Override
public void run() {
for(int x=0;x<100;x++) {
this.msg.get();
}
}
}
public class Threadh {
public static void main(String[] args) {
// TODO Auto-generated method stub
Message msg=new Message();
new Thread(new Productor(msg)).start();
new Thread(new Customer(msg)).start();
}


}
解决了数据的错位,但是重复的操作更严重了
解决数据重复操作:
要想解决重复的操作必须加入等待与唤醒机制。而这样的处理机制是由Object类提供的方法支持,在Object类之中有如下的三个方法使用;
等待:public final void wait() throws InterruptedException;
第一次唤醒:public final void notify();
唤醒全部:public final void notifyAll();谁的优先级高救先执行

范例:解决问题
package thread;
class Message{
private String title;
private String content;
private boolean flag=true;
//flag=true;表示可以生产,但是不能取走
//flag=false;表示可以取走,但是不能生产。
public synchronized void set(String title,String content) {
if(this.flag==false) {
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.title=title;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.content=content;
this.flag=false;
super.notify();
}
public synchronized void get() {
if(this.flag==true) {
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.title+","+this.content);
this.flag=true;
super.notify();
}
}
class Productor implements Runnable{
private Message msg;
public Productor(Message msg) {
this.msg=msg;
}
@Override
public void run() {
for(int x=0;x<100;x++) {
if(x%2==0) {
this.msg.set("小动物","草泥马");
}
else {
this.msg.set("小金子","不是好孩子");
}
}
}
}
class Customer implements Runnable{
private Message msg;
public Customer(Message msg) {
this.msg=msg;
}
@Override
public void run() {
for(int x=0;x<100;x++) {
this.msg.get();
}
}
}
public class Threadh {
public static void main(String[] args) {
// TODO Auto-generated method stub
Message msg=new Message();
new Thread(new Productor(msg)).start();
new Thread(new Customer(msg)).start();
}
}
面试题:请解释sleep()和wait()的区别?
sleep()是Thread类定义的方法,在休眠之后可以自动唤醒。
wait()是Object类定义的方法,等待之后必须使用notify()或notifyAll()手工唤醒
总结:
1.多线程的两种实现方式及区别,
2.同步的概念要清楚
3.知道Object类的三个线程操作方法的作用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值