放苹果java_用Java代码模拟实现:一个人不断往箱子里放苹果,另一个人不

//多生产多消费,生产多个苹果放在容器中,但是该容器只能存放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();

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值