之前的例子都是多个线程执行同一种任务,下面开始讨论多个线程执行不同任务的情况。

举个例子:有个仓库专门存储货物,有的货车专门将货物送往仓库,有的货车则专门将货物拉出仓库,这两种货车的任务不同,而且为了完成任务需要彼此相互合作,如果仓库中没有货物了而将货物拉出仓库的货车先到达了,那么它只有先等待其它货车将货物送入仓库......这种情况和线程间通信的情况很相似。

一、问题的提出-单生产者单消费者模式。

需求:定义一个容器,存储了字段姓名和性别,一个线程0为姓名和性别赋值,赋值完毕之后另一个线程1取走姓名和性别,接着线程0再为姓名和性别赋值,线程1再取走。。。

思路:定义一个资源类Resource存放姓名和性别,定义一个输入线程Input为姓名和性别赋值,定义一个输出线程Output取走姓名和性别。

1.原始代码

 /*
线程安全性问题的产生
*/
class Resource
{
String name;
String sex;
}
class Input implements Runnable
{
Resource r;
public Input(){}
public Input(Resource r)
{
this.r=r;
}
public void run()
{
boolean flag=false;
while(true)
{
if(flag)
{
r.name="Mike";
r.sex="nan";
}
else
{
r.name="丽丽";
r.sex="女女女女女女女女女女女女";
}
flag=!flag;
}
}
}
class Output implements Runnable
{
Resource r;
public Output(){}
public Output(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
System.out.println(r.name+"-----------------"+r.sex);
}
}
}
public class Demo
{
public static void main(String args[])
{
Resource r=new Resource();
Input in=new Input(r);
Output out=new Output(r);
Thread t0=new Thread(in);
Thread t1=new Thread(out);
t0.start();
t1.start();
}
}

运行结果:

2.改进代码,用加锁解决男女性别紊乱问题

观察结果,我们可以发现最严重的问题就是丽丽性别有时候变成了男,而Mike性别有时候变成了女

原因分析:

多线程操作共享资源的代码不止一条。

很明显,对操作共享资源的代码要加上锁,这里使用资源r即可。

我们可不可以对输入输出线程加上不同的锁?

不行。原因:加上锁的目的是为了让访问同一资源的线程保持唯一,也就是说总是要保持只有一个线程访问资源,事实上虽然加了锁,但是CPU也会在未执行完同步代码块之前就切换到其它线程,如果其他线程加了同样的锁,那么由于上一个线程占有了锁,所以CPU无法进入当前的同步代码块,当切换到上一个线程的时候,会在中断处继续执行。这样就保证了访问资源的线程只有一个。本例中由于这个原因,必须加上同一把锁。

 class Resource
{
String name;
String sex;
}
class Input implements Runnable
{
Resource r;
public Input(){}
public Input(Resource r)
{
this.r=r;
}
public void run()
{
boolean flag=false;
while(true)
{
synchronized(r)//使用Resource对象锁,因为对于输入输出线程来说,是唯一的
{
if(flag)
{
r.name="Mike";
r.sex="nan";
}
else
{
r.name="丽丽";
r.sex="女女女女女女女女女女女女";
}
}
flag=!flag;
}
}
}
class Output implements Runnable
{
Resource r;
public Output(){}
public Output(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
synchronized(r)
{
System.out.println(r.name+"-----------------"+r.sex);
}
}
}
}
public class Demo
{
public static void main(String args[])
{
Resource r=new Resource();
Input in=new Input(r);
Output out=new Output(r);
Thread input=new Thread(in);
Thread output=new Thread(out);
input.start();
output.start();
}
}

运行结果:

3.改进代码,用等待唤醒机制解决输出一大片一大片相同数据的情况。

观察运行结果,可以发现虽然性别转为正常,但是输出却还是有问题。怎样改变代码才能解决输出一大片一大片相同数据的情况?

分析:由于线程得到CPU执行权后在时间片用完之前会一直占有,所以会进行多次循环打印。

解决方法:使用等待唤醒机制解决这个问题。

等待唤醒机制中涉及到三个方法:

wait:使得线程进入冻结状态,除非调用notify(可能会唤醒)或者notifyAll方法(一定会唤醒),否则会一直保持冻结状态,注意它将会释放锁。

notify:在当前线程池中唤醒任意一条线程。

notifyAll:唤醒当前线程池中的全部线程。

针对这个问题,使用等待唤醒机制的过程当input线程赋完值之后先唤醒output线程让它取走数据,自己则先睡一会儿等待output线程唤醒自己;output线程取走数据之后,先唤醒input线程,自己再去睡一会儿等待input线程唤醒自己。。。。如此循环即可达到目的。为此需要一个标志变量flag标识当前资源的状态,true标识资源存在,output线程可以取走数据,而input线程则需要等待output线程取走数据;false标识资源为空,input线程可以输入数据,而output线程则要等待input线程输入数据。

 class Resource
{
boolean flag=false;//表名一开始的时候并没有姓名和性别,需要输入。
String name;
String sex;
}
class Input implements Runnable
{
Resource r;
public Input(){}
public Input(Resource r)
{
this.r=r;
}
public void run()
{
boolean flag=false;
while(true)
{
synchronized(r)//使用Resource对象锁,因为对于输入输出线程来说,是唯一的
{
if(r.flag==true)
try
{
r.wait();//让输入线程被等待
}
catch (InterruptedException e){} if(flag)
{
r.name="Mike";
r.sex="nan";
}
else
{
r.name="丽丽";
r.sex="女女女女女女女女女女女女";
}
r.flag=true;
r.notify();//唤醒输出线程。
}
flag=!flag;
}
}
}
class Output implements Runnable
{
Resource r;
public Output(){}
public Output(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
synchronized(r)
{
if(r.flag==false)
try
{
r.wait();//让输出线程被等待
}
catch (InterruptedException e)
{
}
System.out.println(r.name+"-----------------"+r.sex);
r.flag=false;
r.notify();//告诉生产线已经没有货了
}
}
}
}
public class Demo
{
public static void main(String args[])
{
Resource r=new Resource();
Input in=new Input(r);
Output out=new Output(r);
Thread input=new Thread(in);
Thread output=new Thread(out);
input.start();
output.start();
}
}

4.代码优化

观察代码可以发现,资源类中的字段均为友好型的,在实际开发中应当使用私有形变量并且提供对外的访问方法。

代码优化之后:

 class Resource
{
boolean flag=false;//表名一开始的时候并没有姓名和性别,需要输入。
private String name;
private String sex;
public synchronized void set(String name,String sex)
{
if(this.flag==true)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
}
}
this.name=name;
this.sex=sex;
this.flag=true;
this.notify();
}
public synchronized void out()
{
if(this.flag==false)
{
try
{
this.wait();
}
catch (InterruptedException e)
{
}
}
System.out.println(this.name+"---------++--------"+this.sex);
this.flag=false;
this.notify();
}
}
class Input implements Runnable
{
Resource r;
public Input(){}
public Input(Resource r)
{
this.r=r;
}
public void run()
{
boolean flag=false;
while(true)
{
if(flag)
r.set("Mike","nan");
else
r.set("丽丽","女女女女女女女女女"); flag=!flag;
}
}
}
class Output implements Runnable
{
Resource r;
public Output(){}
public Output(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
public class Demo
{
public static void main(String args[])
{
Resource r=new Resource();
Input in=new Input(r);
Output out=new Output(r);
Thread input=new Thread(in);
Thread output=new Thread(out);
input.start();
output.start();
}
}

代码出现了较大的改动,并且代码结构结构变得清晰合理。

二、多生产者、多消费者问题

1.单生产者、单消费者问题

为了便于说明,先从单生产者单消费者问题开始讲解。

需求:生产者生产一个烤鸭,消费者消费一个烤鸭。。

 /*
单生产者单消费者问题,和上一个问题非常相似
*/
class Resource
{
private boolean flag=false;
private String name;
private int count=1;
public synchronized void set(String name )
{
if(this.flag)
try
{
this.wait();
}
catch (InterruptedException e)
{
}
this.name=name+count++;
System.out.println(Thread.currentThread().getName()+"------------生产了"+this.name);
this.flag=true;
this.notify();
}
public synchronized void out()
{
if(!this.flag)
try
{
this.wait();
}
catch (InterruptedException e)
{
}
System.out.println(Thread.currentThread().getName()+"**********************消费了"+this.name);
this.flag=false;
notify();
}
}
class Procedure implements Runnable
{
private Resource r;
public Procedure(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.set("烤鸭");
}
}
} class Consumer implements Runnable
{
private Resource r;
public Consumer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
public class Demo
{
public static void main(String args[])
{
Resource r=new Resource();
Procedure pro=new Procedure(r);
Consumer con=new Consumer(r);
Thread t0=new Thread(pro);
Thread t1=new Thread(con);
t0.start();
t1.start();
}
}

观察上述运行结果,可以发现线程0生产,线程1消费,没有任何问题。

2.多生产者,多消费者问题

如果需要增加生产者、消费者的数量,是不是简单地增加多个线程就可以了?

 /*
现在讨论多生产者、多消费者问题。
1.多生产了没消费的问题。
2.多消费了同一件的问题。
*/
class Resource
{
private boolean flag=false;
private String name;
private int count=1;
public synchronized void set(String name )
{
if(this.flag)
try
{
this.wait();//t0 t1(活)
}
catch (InterruptedException e)
{
}
this.name=name+count++;
System.out.println(Thread.currentThread().getName()+"------------生产了"+this.name);
this.flag=true;
this.notify();
}
public synchronized void out()
{
if(!this.flag)
try
{
this.wait();//t2 t3
}
catch (InterruptedException e)
{
}
System.out.println(Thread.currentThread().getName()+"**********************消费了"+this.name);
this.flag=false;
notify();
}
}
class Procedure implements Runnable
{
private Resource r;
public Procedure(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.set("烤鸭");
}
}
} class Consumer implements Runnable
{
private Resource r;
public Consumer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
public class Demo
{
public static void main(String args[])
{
Resource r=new Resource();
Procedure pro=new Procedure(r);
Consumer con=new Consumer(r);
Thread t0=new Thread(pro);
Thread t1=new Thread(pro);
Thread t2=new Thread(con);
Thread t3=new Thread(con);
t0.start();
t1.start();
t2.start();
t3.start();
}
}

我们可以发现生产了几个烤鸭只有一个被消费或者同一个烤鸭被消费了多次,这是一种线程安全性问题的现象。

3.解决多生产、少消费的情况和消费同一个烤鸭被消费多次的情况

为什么会发生这种现象?

解析我们知道线程0、1只能运行输入线程的代码,2,3只能运行输出线程的代码。

假设线程0开始生产,生产完毕之后进入冻结状态,CPU切换到线程1,线程1发现有烤鸭,所以也进入冻结状态;线程2开始消费,并唤醒线程0,线程0加入堵塞队列,CPU切换到线程3,线程3发现没有烤鸭了,所以也进入堵塞状态。这时候线程池中有三个线程,分别是生产者线程1和消费者线程2、3,能够执行任务的只有线程0,线程0开始运作,它将不再判断flag而直接生产烤鸭,同时唤醒线程池中的一个线程,并进入冻结状态。关键问题就来了,它将会唤醒哪个线程?如果是线程2或者线程3,都不会出现问题,但是如果唤醒了线程1,那么线程1将不再判断flag而直接生产烤鸭,同时唤醒线程池中的一个线程。这时候问题已经很明显了,连续生产了两个烤鸭。如果下一个唤醒的线程是线程2或者3,将会被消费掉一个烤鸭,但是如果唤醒的是刚刚进入冻结状态的线程0,那么将会继续生产烤鸭。。极端情况就是连续一大片都在生产烤鸭而没有消费者消费烤鸭。

同理,连续消费同一个烤鸭多次也就不足为奇了。

解决方法很明显是由于线程醒过来之后没有在此判断flag造成的,所以我们将if改成while即可循环判断

 class Resource
{
private boolean flag=false;
private String name;
private int count=1;
public synchronized void set(String name )
{
while(this.flag)
try
{
this.wait();//t0 t1(活)
}
catch (InterruptedException e)
{
}
this.name=name+count++;
System.out.println(Thread.currentThread().getName()+"------------生产了"+this.name);
this.flag=true;
this.notify();
}
public synchronized void out()
{
while(!this.flag)
try
{
this.wait();//t2 t3
}
catch (InterruptedException e)
{
}
System.out.println(Thread.currentThread().getName()+"**********************消费了"+this.name);
this.flag=false;
notify();
}
}
class Procedure implements Runnable
{
private Resource r;
public Procedure(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.set("烤鸭");
}
}
} class Consumer implements Runnable
{
private Resource r;
public Consumer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
public class Demo
{
public static void main(String args[])
{
Resource r=new Resource();
Procedure pro=new Procedure(r);
Consumer con=new Consumer(r);
Thread t0=new Thread(pro);
Thread t1=new Thread(pro);
Thread t2=new Thread(con);
Thread t3=new Thread(con);
t0.start();
t1.start();
t2.start();
t3.start();
}
}

运行的结果是停在了第四行,光标不断闪烁但没有输出,我们很容易的就发现了,这是一种死锁现象。

4.解决死锁问题。

原因分析:

假设线程0开始生产,生产完毕之后进入冻结状态,CPU切换到线程1,线程1发现有烤鸭,所以也进入冻结状态;线程2开始消费,并唤醒线程0,线程0加入堵塞队列,CPU切换到线程3,线程3发现没有烤鸭了,所以也进入堵塞状态。这时候线程池中有三个线程,分别是生产者线程1和消费者线程2、3,能够执行任务的只有线程0,线程0开始运作(这部分分析和上面的分析相同),判断完flag,它将会生产一个烤鸭,同时唤醒线程池中的一个线程。这时候关键问题就来了,它将唤醒哪个线程?如果是线程2或者3,也没有问题,运行的时候会发现很“和谐”,但是如果唤醒的是线程1,线程1醒来之后会先判断标记,发现标记为true,所以它将继续睡,进入冻结状态,而线程0进入下一次循环并判断标记,也进入冻结状态。所以,现在就发生了四个线程全部处于冻结状态的情况,这也是一种死锁情况。

解决思路:

很明显,由于notify唤醒的线程有可能是对方的线程,但也有可能是己方的线程,当唤醒的是己方的线程时,在一定条件下就会产生死锁。如果我们能够只唤醒对方的线程,那么问题就解决了。但是现在先不讨论那种情况,该怎么解决这个问题?答案就是使用notifyAll方法唤醒全部线程。

 /*
现在讨论多生产者、多消费者问题。
解决多生产者多消费者问题出现的死锁现象:将notify改成notifyAll,这样将会唤醒本方线程和对方线程 */
class Resource
{
private boolean flag=false;
private String name;
private int count=1;
public synchronized void set(String name )
{
while(this.flag)
try
{
this.wait();//t0 t1(活)
}
catch (InterruptedException e)
{
}
this.name=name+count++;
System.out.println(Thread.currentThread().getName()+"------------生产了"+this.name);
this.flag=true;
this.notifyAll();
}
public synchronized void out()
{
while(!this.flag)
try
{
this.wait();//t2 t3
}
catch (InterruptedException e)
{
}
System.out.println(Thread.currentThread().getName()+"**********************消费了"+this.name);
this.flag=false;
notifyAll();
}
}
class Procedure implements Runnable
{
private Resource r;
public Procedure(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.set("烤鸭");
}
}
} class Consumer implements Runnable
{
private Resource r;
public Consumer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
public class Demo
{
public static void main(String args[])
{
Resource r=new Resource();
Procedure pro=new Procedure(r);
Consumer con=new Consumer(r);
Thread t0=new Thread(pro);
Thread t1=new Thread(pro);
Thread t2=new Thread(con);
Thread t3=new Thread(con);
t0.start();
t1.start();
t2.start();
t3.start();
}
}

现象:

现在问题全部解决了。但是很明显,由于notifyAll方法唤醒了全部线程,所以它的效率不高,特别是在线程数量特别多的情况下尤其明显。

三、多生产者、多消费者代码优化。

1.JDK1.5新特性。

JDK1.5针对多线程编程中存在的效率不高的问题做出了优化,特别是针对notifyAll方法,做出了重大改进。

新工具所在包:java.util.concurrent.locks

1.1接口:Lock。

此接口封装了获取锁的方法lock()和释放锁的方法unlock(),这样就将synchronized获取锁和释放锁的隐式过程显式化。

由于Lock是接口名,不能直接new对象,所以要使用实现此接口的已知类,比如ReentrantLock,使用Lock lock=new ReentrantLock();即可得到Lock接口的实例

这样就完成了自定义锁的过程。

应当注意,由于获取锁和释放锁的过程显式化,我们必须将释放锁的动作放在finally块中才能保证线程的安全性。如果线程在未释放所之前发生了异常,那么它将停止程序的运行,并返回上一级调用处,但是它仍然是锁的占有者,这样别的线程将不会有机会访问共享资源。

1.2接口:Condition。

Condition接口封装了三个重要的方法:await() 、signal() 、signalAll(),这三个方法对应着Object类中的wait() 、notify() 、notifyAll()方法。

为什么要使用Condition接口?

由于Lock接口的出现,导致了不使用synchronized关键字也可以,但是自定义锁上并没有这三个重要的方法,这是因为JDK1.5规定了一把锁上可以有多个监视器,如果这三个方法称为Lock接口中的成员,将会使得一把锁上只能有一个监视器,和原来相比就没有了优势。使用Condition接口的最大好处就是可以指定唤醒的线程是对方的上线程还是己方的线程,这样就能大大的提高工作效率

怎么获得实现了Condition接口的对象?

使用Lock接口中的方法:newCondition();

Condition con=lock.newCondition();

2.代码演示

2.1改造成和原来的代码相同的效果。

 /*
现在讨论多生产者、多消费者问题。
使用JDK1.5之后的新特性
改变之后和原来的效果相同,效率没有提高。
*/
import java.util.concurrent.locks.*;
class Resource
{
private boolean flag=false;
private String name;
private int count=1;
Lock lock=new ReentrantLock();
Condition con=lock.newCondition();
public void set(String name )
{
lock.lock();
try
{
while(this.flag)
try
{
con.await();//t0 t1(活)
}
catch (InterruptedException e)
{
}
this.name=name+count++;
System.out.println(Thread.currentThread().getName()+"------------生产了"+this.name);
this.flag=true;
con.signalAll();
}
finally
{
lock.unlock();
}
}
public void out()
{
lock.lock();
try
{
while(!this.flag)
try
{
con.await();//t2 t3
}
catch (InterruptedException e)
{
}
System.out.println(Thread.currentThread().getName()+"**********************消费了"+this.name);
this.flag=false;
con.signalAll();
}
finally
{
lock.unlock();
}
}
}
class Procedure implements Runnable
{
private Resource r;
public Procedure(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.set("烤鸭");
}
}
} class Consumer implements Runnable
{
private Resource r;
public Consumer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
public class Demo
{
public static void main(String args[])
{
Resource r=new Resource();
Procedure pro=new Procedure(r);
Consumer con=new Consumer(r);
Thread t0=new Thread(pro);
Thread t1=new Thread(pro);
Thread t2=new Thread(con);
Thread t3=new Thread(con);
t0.start();
t1.start();
t2.start();
t3.start();
}
}

运行的效果和原来相同,既没有死锁发生,也没有其他线程安全性问题。但是效率和以前完全相同,因为也唤醒了对方的线程。

2.2优化后的代码,只将对方线程唤醒。

 import java.util.concurrent.locks.*;
class Resource
{
private boolean flag=false;
private String name;
private int count=1;
Lock lock=new ReentrantLock();
Condition procedurers=lock.newCondition();
Condition consumers=lock.newCondition();
public void set(String name )
{
lock.lock();
try
{
while(this.flag)
try
{
procedurers.await();//t0 t1(活)
}
catch (InterruptedException e)
{
}
this.name=name+count++;
System.out.println(Thread.currentThread().getName()+"------------生产了"+this.name);
this.flag=true;
consumers.signal();
}
finally
{
lock.unlock();
}
}
public void out()
{
lock.lock();
try
{
while(!this.flag)
try
{
consumers.await();//t2 t3
}
catch (InterruptedException e)
{
}
System.out.println(Thread.currentThread().getName()+"**********************消费了"+this.name);
this.flag=false;
procedurers.signal();
}
finally
{
lock.unlock();
}
}
}
class Procedure implements Runnable
{
private Resource r;
public Procedure(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.set("烤鸭");
}
}
} class Consumer implements Runnable
{
private Resource r;
public Consumer(Resource r)
{
this.r=r;
}
public void run()
{
while(true)
{
r.out();
}
}
}
public class Demo
{
public static void main(String args[])
{
Resource r=new Resource();
Procedure pro=new Procedure(r);
Consumer con=new Consumer(r);
Thread t0=new Thread(pro);
Thread t1=new Thread(pro);
Thread t2=new Thread(con);
Thread t3=new Thread(con);
t0.start();
t1.start();
t2.start();
t3.start();
}
}

运行的效果和以前完全相同,但是效率却要更高。

四、真正的生产者消费者问题。

之前讨论的多生产者多消费者问题所用的资源类中只能存放1个烤鸭,多个生产者争着生产这一个烤鸭,多个消费者争着消费这一个烤鸭,这在实际生活中是不存在的。应当改造成这样:有一个可以存放多个烤鸭的容器,生产者将烤鸭生产完毕之后放入容器,消费者在后面消费,当容器满了,生产者停下等待消费者消费,消费者一旦消费掉一个烤鸭,就告诉生产者容器内可以存放新烤鸭了,生产者于是开始生产新的烤鸭并放在空位上;消费者发现容器空了,则等待生产者生产出烤鸭,生产者一旦生产出烤鸭就立即通知消费者容器内已经有烤鸭了,可以消费了。这才是一个生产者消费者问题的实际流程。

容器可以使用数组,生产者消费者都需要一个指针变量,表示下一个生产出的烤鸭的位置和下一个将要消费的烤鸭所在的位置。此外,应当有一个变量标识当前容器内有多少烤鸭,以便于决定生产者和消费者在容器已满和容器为空的时候的动作。

此外,需要一把同步锁,这是必须的,锁上有两个监视器,这是为了提高效率,原因前面已经解释过。

变量说民:

lock:锁名。

notFull:生产者监视器。

notEmpty:消费者监视器。

items:对象数组,存放“烤鸭”

putptr:指示生产者下一个生产出来的烤鸭应当存放在什么位置。

takeptr:指示消费者下一个将要消费的烤鸭所在的位置。

count:指示当前容器中的烤鸭数量,初始值是0。

容器的大小是5。

1.供大于求

 import java.util.concurrent.locks.*;
class BoundedBuffer {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition(); final Object[] items = new Object[5];
int putptr, takeptr, count=0; public void put(Object x) throws InterruptedException {//生产线
lock.lock();
try {
while (count == items.length)
notFull.await();//生产线已经饱和,无需再生产,等待消费,进入冻结状态。
items[putptr] = x; //生产下一个产品。 System.out.println(Thread.currentThread().getName()+":----生产"+items[putptr]+putptr); if (++putptr == items.length) putptr = 0;//如果生产到了尾部,则从头开始生产。
++count;//数量+1
notEmpty.signal();//唤醒消费者开始消费
} finally {
lock.unlock();
}
} public Object take() throws InterruptedException {
lock.lock();
try {
while (count == 0) //如果生产线上没有货物,则等待生产者生产,进入冻结状态。
notEmpty.await();
Object x = items[takeptr]; //生产线上有货物了,消费货物 System.out.println(Thread.currentThread().getName()+":-----------消费"+items[takeptr]+takeptr); if (++takeptr == items.length) takeptr = 0;//消费到了最后一个产品,则从头开始找货物
--count;//货物数量自减
notFull.signal();//唤醒生产线开始生产
return x;
} finally {
lock.unlock();
}
}
} class Procedure implements Runnable//生产者线程
{
public BoundedBuffer b;
public Procedure(BoundedBuffer b)
{
this.b=b;
}
public void run()
{
while(true)
{
try{
Thread.sleep(1000);
b.put("烤鸭");
}
catch(InterruptedException e)
{
}
}
}
}
class Consumer implements Runnable//消费者线程
{
public BoundedBuffer b;
public Consumer(BoundedBuffer b)
{
this.b=b;
}
public void run()
{
while(true)
{ try
{
Thread.sleep(1000);
b.take();
}
catch(InterruptedException e)
{
}
}
}
}
public class Demo
{
public static void main(String args[])//使用三个生产者和两个消费者以便于观察现象
{
BoundedBuffer b=new BoundedBuffer();
Procedure p=new Procedure(b);
Consumer c=new Consumer(b); Thread t0=new Thread(p);
Thread t1=new Thread(p);
Thread t2=new Thread(p); //Thread t3=new Thread(c);
Thread t4=new Thread(c);
Thread t5=new Thread(c); t0.start();
t1.start();
t2.start();
//t3.start();
t4.start();
t5.start();
}
}

程序采用了生产延时和消费延时的方式,以便于观察现象但是不影响最终结果。

运行结果:

分析上述输出结果,我们可以发现一开始生产者的生产和消费者的消费不平衡,即生产者的生产>消费者的消费,但是后来生产者的生产和消费者的消费就达到了平衡,即生产者生产一个,消费者消费一个。这句话听起来没有问题,但其实是错误的,事实上是消费者消费掉一个,生产者生产出一个原因就是在本程序中,生产者为3个,而消费者为2个,本程序没有给线程定义优先级,所以他们得到CPU的执行权的概率基本相同(生产能力和消费能力相同,此次分析建立在这个基础之上)。所以程序很快就进入了饱和状态。即最开始的时候生产者已经将容器填满,等消费者消费掉一个,生产者就生产一个。我们可以发现,当稳定的时候,消费者消费烤鸭的标号总是==生产者生产烤鸭的编号+1,这就是供大于求的证明。事实上这时候容器一直处于满的状态。

举一个不太恰当的例子:操场上两个人甲乙一起跑步,甲跑的快,很快就跑完了两圈,但是这时候乙才跑完了一圈,这时候甲乙就又碰面了,貌似是跑的一样快,但是其实甲已经多跑了一圈,本应当超过乙继续跑,但是乙却挡在甲的前面不让甲过去,这样甲就不得不让乙先跑,乙跑一步,甲就在后面跟着乙跑一步。在这个例子中,甲相当于生产者,乙相当于消费者,这是供大于求的情况。

2.供小于求

我们将生产者的数量改成2,将消费者的数量改为3,再看看结果怎么样。

 import java.util.concurrent.locks.*;
class BoundedBuffer {
final Lock lock = new ReentrantLock();
final Condition notFull = lock.newCondition();
final Condition notEmpty = lock.newCondition(); final Object[] items = new Object[5];
int putptr, takeptr, count=0; public void put(Object x) throws InterruptedException {//生产线
lock.lock();
try {
while (count == items.length)
notFull.await();//生产线已经饱和,无需再生产,等待消费,进入冻结状态。
items[putptr] = x; //生产下一个产品。 System.out.println(Thread.currentThread().getName()+":----生产"+items[putptr]+putptr); if (++putptr == items.length) putptr = 0;//如果生产到了尾部,则从头开始生产。
++count;//数量+1
notEmpty.signal();//唤醒消费者开始消费
} finally {
lock.unlock();
}
} public Object take() throws InterruptedException {
lock.lock();
try {
while (count == 0) //如果生产线上没有货物,则等待生产者生产,进入冻结状态。
notEmpty.await();
Object x = items[takeptr]; //生产线上有货物了,消费货物 System.out.println(Thread.currentThread().getName()+":-----------消费"+items[takeptr]+takeptr); if (++takeptr == items.length) takeptr = 0;//消费到了最后一个产品,则从头开始找货物
--count;//货物数量自减
notFull.signal();//唤醒生产线开始生产
return x;
} finally {
lock.unlock();
}
}
} class Procedure implements Runnable//生产者线程
{
public BoundedBuffer b;
public Procedure(BoundedBuffer b)
{
this.b=b;
}
public void run()
{
while(true)
{
try{
Thread.sleep(1000);
b.put("烤鸭");
}
catch(InterruptedException e)
{
}
}
}
}
class Consumer implements Runnable//消费者线程
{
public BoundedBuffer b;
public Consumer(BoundedBuffer b)
{
this.b=b;
}
public void run()
{
while(true)
{ try
{
Thread.sleep(1000);
b.take();
}
catch(InterruptedException e)
{
}
}
}
}
public class Demo
{
public static void main(String args[])//使用三个生产者和两个消费者以便于观察现象
{
BoundedBuffer b=new BoundedBuffer();
Procedure p=new Procedure(b);
Consumer c=new Consumer(b); Thread t0=new Thread(p);
Thread t1=new Thread(p);
//Thread t2=new Thread(p); Thread t3=new Thread(c);
Thread t4=new Thread(c);
Thread t5=new Thread(c); t0.start();
t1.start();
//t2.start();
t3.start();
t4.start();
t5.start();
}
}

这个现象就很容易解释了,因为生产者的数量为2<消费者的数量3,所以一开始的时候就出现了供不应求的情况。这时候的现象就是生产者生产出一个,消费者就消费掉一个,我们可以看到生产者生产出的烤鸭编号总是等于消费者消费的烤鸭编号,这就是供不应求的证明

再举一个不太恰当地例子:监跑老师的运动能力很强,他要和学生一起跑以监督学生的跑步质量。监跑老师一直跟在学生后面催着学生赶快跑,学生跑一步,监跑老师就跑一步。

【JAVA线程间通信技术】的更多相关文章

  1. Java 线程间通讯(共享变量方式)

    Java线程间通讯,最常用的方式便是共享变量方式,多个线程共享一个静态变量就可以实现在线程间通讯,但是这需要注意的就是线程同步问题. 一.没考虑线程同步: package com.wyf; publi ...

  2. Java线程间通信-回调的实现方式

    Java线程间通信-回调的实现方式   Java线程间通信是非常复杂的问题的.线程间通信问题本质上是如何将与线程相关的变量或者对象传递给别的线程,从而实现交互.   比如举一个简单例子,有一个多线程的 ...

  3. 说说Java线程间通信

    序言 正文 [一] Java线程间如何通信? 线程间通信的目标是使线程间能够互相发送信号,包括如下几种方式: 1.通过共享对象通信 线程间发送信号的一个简单方式是在共享对象的变量里设置信号值:线程A在 ...

  4. 说说 Java 线程间通信

    序言 正文 一.Java线程间如何通信? 线程间通信的目标是使线程间能够互相发送信号,包括如下几种方式: 1.通过共享对象通信 线程间发送信号的一个简单方式是在共享对象的变量里设置信号值:线程A在一个 ...

  5. Java线程间通信方式剖析——Java进阶(四)

    原创文章,同步发自作者个人博客,转载请在文章开头处以超链接注明出处 http://www.jasongj.com/java/thread_communication/ CountDownLatch C ...

  6. Java 线程间通讯(管道流方式)

    一.管道流是JAVA中线程通讯的常用方式之一,基本流程如下: 1)创建管道输出流PipedOutputStream pos和管道输入流PipedInputStream pis 2)将pos和pis匹配 ...

  7. Java线程间通信之wait/notify

    Java中的wait/notify/notifyAll可用来实现线程间通信,是Object类的方法,这三个方法都是native方法,是平台相关的,常用来实现生产者/消费者模式.我们来看下相关定义: w ...

  8. java线程间通信:一个小Demo完全搞懂

    版权声明:本文出自汪磊的博客,转载请务必注明出处. Java线程系列文章只是自己知识的总结梳理,都是最基础的玩意,已经掌握熟练的可以绕过. 一.从一个小Demo说起 上篇我们聊到了Java多线程的同步 ...

  9. Java线程间和进程间通信

    1 线程与线程间通信 1.1 基本概念以及线程与进程之间的区别联系 关于进程和线程,首先从定义上理解就有所不同: 进程是具有一定独立功能的程序.它是系统进行资源分配和调度的一个独立单位,重点在系统调度 ...

随机推荐

  1. java计算时间差

    比如:现在是2016-03-26 13:31:40        过去是:2016-01-02 11:30:24 我现在要获得两个日期差,差的形式为:XX天XX小时XX分XX秒 方法一: DateFo ...

  2. VQuery高级特性

    VQuery高级特性 css方法 同时设置多个--for in 链式操作 链式操作 函数,链式操作 css 方法链式操作 json的使用 阻止冒泡,默认事件 VQuery插件 插件机制 可以扩展库的功 ...

  3. silverlight 获取服务器上图片出现异常 “AG_E_NETWORK_ERROR”

    前言 之前项目一直是发布在IIS上面使用HTTP访问,现在要求改为HTTPS,通过在IIS生成自签名后,打开HTTPS通道,可以将原来的程序已HTTPS的方式发布出来. 可参见 http://blog ...

  4. chrome调试命令模式

    哈哈哈

  5. ios Swift 一些注意事项

    func test(one:NSString) -> NSString{ return "aaa" } func test(one:Int) -> NSString{ ...

  6. Mybatis中的in查询和foreach标签

    Mybatis中的foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separato ...

  7. FFmpeg-20160413-snapshot-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 F ...

  8. Python~函数的参数

    def func(a,b,c,*args,**kw): print('a=',a,'b=',b,'c=',c,'args=',args,'kw=',kw) 必选参数,默认参数,可变参数,关键字参数 d ...

  9. codeforces 507B. Painting Pebbles 解题报告

    题目链接:http://codeforces.com/problemset/problem/509/B 题目意思:有 n 个piles,第 i 个 piles有 ai 个pebbles,用 k 种颜色 ...

  10. 当你的IIS需要运行ASP网站时,需要这样配置下你的IIS

    1.进入Windows 7的 控制面板->程序和功能->选择左上角的 打开或关闭Windows功能 2.现在出现了安装Windows功能的选项菜单,注意选择的项目,红色箭头所示的地方都要选 ...