wait、notifyall、synchronized实现生产消费模式

资源类:

package test.consumerProducer;

public class Resource {
	//当前资源池数量
    private int currentSize = 0;
    //允许数量
    private int allowSize = 10;

    // 取走资源,如果当前资源大于0则可以移除(消费),移除之后唤醒生产线程。否则进入等待释放线程资源
    public synchronized void remove() {
        if (currentSize > 0) {
            currentSize--;
            System.out.println(Thread.currentThread().getName() + "消费一件资源,当前资源池有" + currentSize + "个");

            notifyAll();
        } else {

            // 没有资源 消费者进入等待状态
            try {
                System.out.println(Thread.currentThread().getName() + "当前资源过少,等待增加");
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
    
    public synchronized void add() {
        // 如果当前数量小于限制数量则可以增加,增加后唤醒消费者消费,否则等待消费,释放锁
        if (currentSize < allowSize) {
            currentSize++;
            System.out.println(Thread.currentThread().getName() + "生产一件资源,当前资源池有" + currentSize + "个");
            notifyAll();

        } else {

            try {
                System.out.println(Thread.currentThread().getName() + "当前资源过多,等待消费");
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

    }
}

生产者:

 

package test.consumerProducer;

public class ProducerThread extends Thread {
	private Resource resource;

    ProducerThread(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //生产
            resource.add();
        }

    }
}

消费者:

package test.consumerProducer;

public class ConsumerThread extends Thread {
	private Resource resource;

    ConsumerThread(Resource resource) {
        this.resource = resource;
    }

    @Override
    public void run() {
        while (true) {
            //避免生产消费太快测试的时候看不到打印,休眠一秒
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //移除代表消费
            resource.remove();
        }

    }
}

 

主类:

package test.consumerProducer;

public class ProducerConsumer {
	public static void main(String[] arg) {

        Resource resource = new Resource();
        // 生产者线程
        ProducerThread p1 = new ProducerThread(resource);
        ProducerThread p2 = new ProducerThread(resource);
        ProducerThread p3 = new ProducerThread(resource);
        // 消费者线程。测试时可以少开几个消费线程看看具体
        ConsumerThread c1 = new ConsumerThread(resource);
        ConsumerThread c2 = new ConsumerThread(resource);
        ConsumerThread c3 = new ConsumerThread(resource);

        p1.start();
        p2.start();
        p3.start();
        c1.start();
        c2.start();
        c3.start();

    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值