欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 学习笔记 使用并发包ReentrantLock简化生产者消费者模式代码

程序员文章站 2022-03-30 21:51:30
说明 ReentrantLock是java官方的一个线程锁类,ReentarntLock实现了Lock的接口 我们只需要使用这个,就可以不用使用synchronized同步关键字以及对应的notifyall方法和wait方法去写我们的生产者消费者模式。 这样就可以大大简化我们的代码 代码 Produ ......

说明

reentrantlock是java官方的一个线程锁类,reentarntlock实现了lock的接口
我们只需要使用这个,就可以不用使用synchronized同步关键字以及对应的notifyall方法和wait方法去写我们的生产者消费者模式。
这样就可以大大简化我们的代码

代码

producer.java

package producterandcustomer;

import java.util.list;
import java.util.concurrent.locks.lock;

/**
 * @author starsone
 * @date create in  2019-4-13 0013 14:51:21
 * @description
 */
class producter extends thread {
    list<object> warehouse;
    private string name;
    lock lock;

    public producter(list<object> warehouse, string name, lock lock) {
        this.warehouse = warehouse;
        this.name = "生产者" + name;
        //多态,reentarntlock实现了lock的接口,所以可以传进来
        this.lock = lock;

    }

    @override
    public void run() {
        while (true) {
            //加锁
            lock.lock();
            try {
                if (warehouse.size() < 10) {
                    warehouse.add(new object());
                    system.out.println(name + "生产1件" + "当前仓库有" + warehouse.size() + "件");
                }
            } finally {
                //解锁
                lock.unlock();
            }

            try {
                sleep(200);
            } catch (interruptedexception e) {
                e.printstacktrace();
            }
        }
    }
}

consumer.java

package producterandcustomer;

import java.util.list;
import java.util.concurrent.locks.lock;

/**
 * @author starsone
 * @date create in  2019-4-13 0013 14:51:21
 * @description
 */
class cuotmer extends thread{
    list<object> warehouse;
    private string name;
    lock lock;

    public cuotmer(list<object> warehouse, string name, lock lock) {
        this.warehouse = warehouse;
        this.name = "消费者"+name;
        this.lock = lock;
    }

    @override
    public void run() {
        while (true) {
            //加锁
            lock.lock();
            try {
                if (warehouse.size() > 0) {
                    warehouse.remove(0);
                    system.out.println(name+"消费1件"+"当前仓库有"+warehouse.size()+"件");
                }
            }finally {
                //最后释放
                lock.unlock();
            }

            try {
                sleep(200);
            } catch (interruptedexception e) {
                e.printstacktrace();
            }
        }
    }
}

test.java

package producterandcustomer;

import java.util.arraylist;
import java.util.list;
import java.util.concurrent.locks.lock;
import java.util.concurrent.locks.reentrantlock;

/**
 * @author starsone
 * @date create in  2019-4-13 0013 14:57:26
 * @description
 */
class test {
    public static void main(string[] args) {
        list<object> warehouse = new arraylist<>();
        lock lock = new reentrantlock();
        new cuotmer(warehouse, "小米", lock).start();
        new cuotmer(warehouse, "小蓝", lock).start();
        new cuotmer(warehouse, "小浩", lock).start();
        new cuotmer(warehouse, "小家", lock).start();
        new cuotmer(warehouse, "小留", lock).start();

        new producter(warehouse, "小一", lock).start();
        new producter(warehouse, "小二", lock).start();
        new producter(warehouse, "小香", lock).start();
        new producter(warehouse, "小叫", lock).start();
        new producter(warehouse, "小三", lock).start();


    }
}