本文共 3696 字,大约阅读时间需要 12 分钟。
//多生产多消费,生产多个苹果放在容器中,但是该容器只能存放5个苹果
//公共资源类
import java.util.LinkedList;
class Box
{
private final int MAX_SIZE=5;//仓储最大容量
private LinkedList list = new LinkedList();//仓储容器
//增加公共资源
public void increase(){
synchronized(list){
while(list.size() > MAX_SIZE){
System.out.println("...现在箱子中苹果的数量:"+list.size()+
"箱子中的苹果超过5个,暂时不能执行任务!");
try{list.wait();}catch(InterruptedException e ){e.printStackTrace();}
}
list.add(new Object());
System.out.println("...往箱子中放入1个苹果,现在箱子中苹果的数量:"+list.size());
list.notifyAll();
}
}
//减少公共资源
public void decrease(){
synchronized(list){
while(list.size()<1){
System.out.println("现在箱子中苹果的数量:"+list.size()+
"箱子中苹果数量不足1个,暂时不能执行生产任务!");
try{list.wait();} catch(InterruptedException e ){e.printStackTrace();}
}
list.remove();//从集合中移除
System.out.println("从箱子中取出1个苹果,现在箱子中苹果的数量:"+list.size());
list.notifyAll();
}
}
}
//生产者
class Producer implements Runnable
{
private Box box;
Producer(Box box){
this.box = box;
}
public void run(){
while(true){
try
{
Thread.sleep((long)(Math.random()*1000));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
box.increase();
}
}
}
//消费者
class Consumer implements Runnable
{
private Box box;
public Consumer(Box box){
this.box = box;
}
public void run(){
while(true){
try
{
Thread.sleep((long)(Math.random()*1000));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
box.decrease();
}
}
}
class BoxAppleThread
{
public static void main(String[] args)
{
//创建资源对象
Box box = new Box();
//创建线程任务对象
Producer pro = new Producer(box);
Consumer con = new Consumer(box);
//创建线程
new Thread(pro).start();
new Thread(con).start();
new Thread(pro).start();
new Thread(con).start();
}
}
--------------------------------------------------------------------------------------------------------------------------------------
//多生产多消费,生产的苹果放容器中,但是容器中只能存放5个苹果
//JDK1.5后使用Lock代替同步
//公共资源类
import java.util.LinkedList;
import java.util.concurrent.locks.*;
class Box
{
private final int MAX_SIZE = 5;//容器中存储最大容量
private LinkedList list = new LinkedList();//存储容器
//创建锁对象
private final Lock lock= new ReentrantLock();
//创建监视器对象
private final Condition produce = lock.newCondition();
private final Condition consume = lock.newCondition();
//生产公共资源
public void increase(){
//获取锁
lock.lock();
try
{
while(list.size() > MAX_SIZE){
System.out.println("...现在箱子中苹果的数量:"+list.size()+
"箱子中的苹果超过5个,暂时不能执行任务!");
try{produce.await();} catch(InterruptedException e){e.printStackTrace();}
}
list.add(new Object());
System.out.println("...往箱子中放入1个苹果,现在箱子中苹果的数量:"+list.size());
consume.signal();//唤醒消费者
}
finally
{
//释放锁
lock.unlock();
}
}
//消费公共资源
public void decrease(){
//获取锁
lock.lock();
try
{
while(list.size() < 1){
System.out.println("现在箱子中苹果的数量:"+list.size()+
"箱子中苹果数量不足1个,暂时不能执行生产任务!");
try{consume.await();} catch(InterruptedException e){e.printStackTrace();}
}
list.remove();
System.out.println("从箱子中取出1个苹果,现在箱子中苹果的数量:"+list.size());
produce.signal();//唤醒生产者
}
finally
{
//释放锁
lock.unlock();
}
}
}
//生产者类
class Producer implements Runnable
{
private Box box;
Producer(Box box){
this.box = box;
}
public void run(){
while(true){
try
{
Thread.sleep((long)(Math.random()*1000));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
box.increase();
}
}
}
//消费者类
class Consumer implements Runnable
{
private Box box;
Consumer(Box box){
this.box = box;
}
public void run(){
while(true){
try
{
Thread.sleep((long) (Math.random()*1000));
}
catch (InterruptedException e)
{
e.printStackTrace();
}
box.decrease();
}
}
}
public class BoxAppleThread2
{
public static void main(String[] args)
{
//创建资源类对象
Box box = new Box();
//创建线程任务对象
Producer pro = new Producer(box);
Consumer con = new Consumer(box);
//创建线程
new Thread(pro).start();
new Thread(con).start();
}
}
转载地址:http://htspa.baihongyu.com/