java多线程:Lock实现生产者和消费者

执行结果如下:
在这里插入图片描述

import java.util.LinkedList;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class newProducer extends Thread
{
    AtomicInteger count;
    Lock lock;
    Condition condition;
    int limit = 200;
    public newProducer(String name, AtomicInteger count, Lock lock, Condition condition)
    {
        super(name);
        this.count = count;
        this.lock = lock;
        this.condition = condition;
    }
    @Override
    public void run() {
        while(true)
        {
            lock.lock();//获取锁//并占有从lock()之后到unlock()之前的代码中的所有对象
            try {


                while (count.intValue() == limit) {
                    condition.await();
                }

                System.out.println(this.getName()+" 生产1件商品,商品总量为 "+ count.addAndGet(1));
                condition.signalAll();//通知所有等待获取锁的线程,锁即将被释放

            }
            catch (InterruptedException e){e.printStackTrace();}
            finally {

                lock.unlock();

            }
        }

    }
}

class newConsumer extends Thread
{
    AtomicInteger count;
    Lock lock;
    Condition condition;

    public newConsumer(String name,AtomicInteger count, Lock lock, Condition condition)
    {
        super(name);
        this.count = count;
        this.lock = lock;
        this.condition = condition;
    }
    @Override
    public void run() {
        while(true)
        {
            lock.lock();//争抢锁并获取锁
            try {

                while (count.intValue() == 0) {
                    condition.await();
                }

                System.out.println(this.getName()+"消费了1件商品,商品总量为 "+ count.addAndGet(-1));
                condition.signalAll();

            }
            catch (InterruptedException e){e.printStackTrace();}
            finally {
                lock.unlock();

            }
        }
    }
}

public class locktest {
    public static void main(String args[])
    {
        AtomicInteger count = new AtomicInteger();//count的自加和自减操作都将为原子操作
        count.addAndGet(0);
        Lock lock = new ReentrantLock();
        Condition condition = lock.newCondition();

        newProducer producer1 = new newProducer("生产者线程1",count,lock,condition);
        newProducer producer2 = new newProducer("生产者线程2",count,lock,condition);
        newConsumer consumer1 = new newConsumer("消费者线程1",count,lock,condition);
        newConsumer consumer2 = new newConsumer("消费者线程2",count,lock,condition);
        newConsumer consumer3 = new newConsumer("消费者线程3",count,lock,condition);
        producer1.start();
        producer2.start();
        consumer1.start();
        consumer2.start();
        consumer3.start();

    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值