您的位置:首页 > 编程语言 > Java开发

java线程协作,经典生产者/消费者模式(一、synchronized互斥)

2016-06-20 22:24 531 查看
java多线程协作,最经典的就是生产者/消费者模式,,比如,一个盘子(缓冲区),假如这个盘子最多只能放一个苹果。现在如果我要从盘里拿出一个苹果,首先要保证盘里有苹果,如果没有,就要等到放入了才能拿;同样,我如果要放一个苹果到盘子里,就要保证盘子里面现在没有苹果,如果有,就要等拿走了又才能放。下面贴上实现放入苹果/拿出苹果这个过程的线程代码:

/**
* java线程协作,生产者/消费者
* @author 李小拐
* 2016年6月20日20:36:56
*/
public class Plate {

private static int plate=0;//声明一个变量,代表盘子,可以放入和拿出
//放入一个苹果,使用synchronized关键字
public synchronized void putApple() throws InterruptedException {
while (plate==1) {
this.wait();//如果有苹果,不能再放了,就等待
}
System.out.println("放入apple");
plate=1;
this.notifyAll();//放入后,唤醒等待的线程
}
//拿出一个苹果,使用synchronized关键字
public synchronized void getApple() throws InterruptedException {
while (plate==0) {
this.wait();//如果盘子为空,则不能取,等待放入苹果才能取
}
System.out.println("拿出苹果");
plate=0;
this.notifyAll();//取出后,唤醒等待线程
}

public static void main(String[] args) {
Plate plate=new Plate();
for (int i = 0; i < 200; i++) {//put和get各执行20次
new getApple(plate).start();
new putApple(plate).start();
}
}
}
//拿出苹果的线程
class getApple extends Thread{
private Plate plate;
public getApple(Plate plate) {
this.plate=plate;
}
@Override
public void run() {
try {
plate.getApple();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

//放入苹果的线程
class putApple extends Thread{
private Plate plate;
public putApple(Plate plate) {
this.plate=plate;
}
@Override
public void run() {
try {
plate.putApple();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


以上这个列子就是经典的生产者/消费者模式,如果有不正确的地方,请看见的大神指出来,以免误导了后面学习的人。

下面,在贴一段经典面试题的代码:大致需求如下,开启ABC三个线程,A打印A,B打印B,C打印C,ABC三个线程同时启动,顺序输入ABC,如此循环10次。

/**
* 生产者/消费者 :顺序打印ABCD
* @author 李小拐
* 2016年6月20日21:00:36
*/
public class PrintABCD {
/**
* @param args
*/
public static void main(String[] args) {
//一定是在这里new一个对象,不能再线程里面new,否则多个线程就不是操作的同一个内存对象
PrintABCD abcd=new PrintABCD();

for (int i = 0; i < 10; i++) {
(new printA(abcd)).start();
(new printB(abcd)).start();
(new printC(abcd)).start();
(new printD(abcd)).start();
}
}

private static int who=1;//用变量who来控制要打印的值;1,2,3,4分别代表A,B,C,D

public synchronized void printA() throws InterruptedException {
while (who!=1) {
this.wait();//不等于1就等待,等于1的时候才执行
}
System.out.println("A");
who=2;//A的下一状态B
this.notifyAll();
}

public synchronized void printB() throws InterruptedException {
while (who!=2) {
this.wait();
}
System.out.println("--B");
who=3;//B的下一状态C
this.notifyAll();
}

public synchronized void printC() throws InterruptedException {
while (who!=3) {
this.wait();
}
System.out.println("----C");
who=4;//C的下一状态D
this.notifyAll();
}

public synchronized void printD() throws InterruptedException {
while (who!=4) {
this.wait();
}
System.out.println("------D");
who=1;//D的下一状态A
this.notifyAll();
}
}
//四个线程,分别打印ABCD
class printA extends Thread{
PrintABCD p;
printA(PrintABCD p){
this.p=p;
}
@Override
public void run() {
try {
p.printA();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class printB extends Thread{
PrintABCD p;
printB(PrintABCD p){
this.p=p;
}
@Override
public void run() {
try {
p.printB();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class printC extends Thread{
PrintABCD p;
printC(PrintABCD p){
this.p=p;
}
@Override
public void run() {
try {
p.printC();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class printD extends Thread{
PrintABCD p;
printD(PrintABCD p){
this.p=p;
}
@Override
public void run() {
try {
p.printD();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}


这里打印了ABCD,其实和ABC是一样的,只是demo里面多写了一个。synchronized关键字保证了线程的互斥。JDK1.5以前基本都使用synchronized关键字进行线程间协作,JDK1.5以后提供了更高级的通信方式,我将在下一篇文章贴出demo代码。

最后一句,学无止境,文章若有不足之处,请大神们及时指正,,纠正了我,也及时避免误导后人
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息