描述SYnchronizedReentrantLock的区别?

Posted top啦它

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了描述SYnchronizedReentrantLock的区别?相关的知识,希望对你有一定的参考价值。

https://www.bilibili.com/video/BV1f4411y7TJ,讲的很好


ReenTrantLock生产者,消费者。

package shengxiao;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Changku 
    private static final int MAX_COUNT = 1000;
    private List<Production> list = new ArrayList<Production>();
    private final Lock lock = new ReentrantLock();
    private final Condition condition = lock.newCondition();
    private int index;

    public void producer()
        try
            lock.lock();
            if (list.size()>=MAX_COUNT)
                System.out.println("仓库满了,等待消费。");
                condition.await();
            
//            Thread.sleep(500);
            Production production = new Production(index++);
            list.add(production);
            System.out.println("生产一个");
            condition.signal();
        catch (Exception e)
            e.printStackTrace();
        finally 
            lock.unlock();
        
    

    public void cumsumer()
        try
            lock.lock();
            if (list.size()<=0)
                System.out.println("货物不足,等待生产");
                condition.await();
            
//            Thread.sleep(500);
            Production remove = list.remove(0);
            System.out.println("消费一个");
            condition.signal();
        catch (Exception e)
            e.printStackTrace();
        finally 
            lock.unlock();
        
    


    public static class Production
        public int index;

        public Production(int index) 
            this.index = index;
        

        @Override
        public String toString() 
            return "Produce" +
                    "index=" + index +
                    '';
        
    


package shengxiao;

public class Xiaofei extends Thread
    Changku changku;

    public Xiaofei(Changku changku) 
        this.changku = changku;
    

    @Override
    public void run() 
        while (!Thread.interrupted())
            changku.cumsumer();
            try 
                Thread.sleep(1000);
             catch (InterruptedException e) 
                e.printStackTrace();
            
        
    


package shengxiao;

public class Shengcan extends Thread
    Changku changku;

    public Shengcan(Changku changku) 
        this.changku = changku;
    

    @Override
    public void run() 
        while (!Thread.interrupted())
            changku.producer();
            try 
                Thread.sleep(1000);
             catch (InterruptedException e) 
                e.printStackTrace();
            
        
    


package shengxiao;

public class Yunxing 
    public static void main(String[] args) 
        Changku changku = new Changku();
        Shengcan shengcan = new Shengcan(changku);
        Xiaofei xiaofei = new Xiaofei(changku);
        shengcan.start();
        xiaofei.start();
    


以上是关于描述SYnchronizedReentrantLock的区别?的主要内容,如果未能解决你的问题,请参考以下文章

USB学习笔记连载:USB描述符

什么叫算法描述啊

什么是图像处理(算法或描述)中的特征描述符?

USB HID描述符

描述符

python——描述符