问题引出:
生产者和消费者是2个不同的线程类对象,操作同一资源的情况,具体操作流程如下:
- 生产者负责生产数据,消费者负责取走数据;
- 生产者每生产一组数据后,消费者就要取走这组数据;
范例:数据模型
class Info {
private String tite;
private String content;
public String getTite() {
return tite;
}
public void setTite(String tite) {
this.tite = tite;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
class Productor implements Runnable {
private Info info;
public Productor(Info info) {
this.info = info;
}
@Override
public void run() {
for (int x = 0; x < 100; x++) {
if (x % 2 == 0) { // 偶数
this.info.setTite("罗XX");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.info.setContent("大笨蛋");
} else {
this.info.setTite("金XX");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.info.setContent("小可爱");
}
}
}
}
class Customer implements Runnable {
private Info info;
public Customer(Info info) {
this.info = info;
}
@Override
public void run() {
for (int x = 0; x < 200; x++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.info.getTite() + "-" + this.info.getContent());
}
}
}
public class test {
public static void main(String args[]) throws Exception {
Info info = new Info();
new Thread(new Productor(info)).start();
new Thread(new Customer(info)).start();
}
}
通过上述代码会发现2个严重问题:
- 数据错位:发现不再是一个所需要的完整数据;
- 数据重复取出,数据重复设置;
同步处理——数据错乱:
数据错乱完全是因为非同步操作造成的,所以应该使用同步处理;也就是设置和取是一个动作,应该放在一个类里面;
class Info {
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(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(this.title + "-" + this.content);
}
}
class Productor implements Runnable {
private Info info;
public Productor(Info info) {
this.info = info;
}
@Override
public void run() {
for (int x = 0; x < 100; x++) {
if (x % 2 == 0) { // 偶数
this.info.set("罗XX", "大笨蛋");
} else {
this.info.set("金XX", "小可爱");
}
}
}
}
class Customer implements Runnable {
private Info info;
public Customer(Info info) {
this.info = info;
}
@Override
public void run() {
for (int x = 0; x < 200; x++) {
this.info.get();
}
}
}
public class test {
public static void main(String args[]) throws Exception {
Info info = new Info();
new Thread(new Productor(info)).start();
new Thread(new Customer(info)).start();
}
}
所以数据错乱问题解决了,但是重复问题变得更严重了;
同步处理——数据重复:
生产者生产了一个数据,放入公共区,此时指示灯变为红色,表示公共区已有数字等待取走,当消费者取走公共区的数字后,指示灯变为绿色,表示生产者可以向公共区生产数据...如果想实现整个流程,需要加入等待与唤醒机制,在Object类里有专门的方法:
- 等待:public final void wait() throws InterruptedException;
- 唤醒第一个等待线程:public final void notify();
- 唤醒全部线程,优先级高的先执行:public final void notifyAll();
范例:解决
class Info {
private String title;
private String content;
private boolean flag = true;
// flag = true; 表示可以生产但是不可以取走;
// flag = false; 表示可以取走但是不可以生产;
public synchronized void set(String title, String content) {
// 重复进入set()方法里面,发现不可以生产,所以要等待
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(100);
} 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 Info info;
public Productor(Info info) {
this.info = info;
}
@Override
public void run() {
for (int x = 0; x < 100; x++) {
if (x % 2 == 0) { // 偶数
this.info.set("罗XX", "大笨蛋");
} else {
this.info.set("金XX", "小可爱");
}
}
}
}
class Customer implements Runnable {
private Info info;
public Customer(Info info) {
this.info = info;
}
@Override
public void run() {
for (int x = 0; x < 200; x++) {
this.info.get();
}
}
}
public class test {
public static void main(String args[]) throws Exception {
Info info = new Info();
new Thread(new Productor(info)).start();
new Thread(new Customer(info)).start();
}
}
面试题:请解释sleep()与wait()的区别?
- sleep()是Thread类定义的方法,wait()是Object类定义的方法;
- sleep()可以设置休眠时间,时间一到自动唤醒,而wait()需要等待notify()进行唤醒;