Company code scanning gun technology --- use Semaphore for data verification (create string)

In the development of our company's project: when using the scene scanning gun to scan the code, we scan the code at multiple venues at the same time and enter the library to
verify the correctness of the different QR codes. The function of this experiment is that there are several threads that can access the pool Data,
but only one thread can obtain the data for verification at the same time, and only verify the correctness of the data, and then return to the front-end data later,

package com.zcw.demo1;

/**
 * @ClassName : MyThread
 * @Description :
 * @Author : Zhaocunwei
 * @Date: 2020-04-14 16:06
 */
public class MyThread extends Thread {
    private TestListPool testListPool;
    public MyThread(TestListPool testListPool){
        super();
        this.testListPool = testListPool;
    }
    @Override
    public void run(){
        for(int i =0; i<Integer.MAX_VALUE;i++){
            String getString = testListPool.get();
            System.out.print(Thread.currentThread().getName()+"取得值" +getString+"\n");
            testListPool.put(getString);
        }
    }

    public static void main(String[] args) {
        TestListPool testListPool = new TestListPool();
        MyThread[] threads = new MyThread[12];

        for(int i =0;i<threads.length;i++){
            threads[i] = new MyThread(testListPool);
        }
        for(int i =0;i<threads.length;i++){
            threads[i].start();
        }
    }
}
package com.zcw.demo1;

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

/**
 * @ClassName : TestListPool
 * @Description : 使用Semaphore创建字符串池,若干个线程可以访问池中的数据,但同时只有一个线程可以取得数据
 * @Author : Zhaocunwei
 * @Date: 2020-04-14 15:26
 */
public class TestListPool {

    private int poolMaxSize=3;
    private int semaphorePermits =5;
    private List<String> list = new ArrayList<String>();
    private Semaphore concurrencySemaphore = new Semaphore(semaphorePermits);
    private ReentrantLock lock  = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public TestListPool(){
        super();
        for (int i=0;i<poolMaxSize;i++){
            list.add("测试数据"+(i+1));
        }
    }
    public String get(){
        String getString = null;
        try{
            concurrencySemaphore.acquire();
            lock.lock();
            while(list.size() ==0){
                condition.await();
            }
            getString = list.remove(0);
            lock.unlock();
        }catch (InterruptedException e) {
            e.printStackTrace();
        }
        return getString;
    }
    public void put(String  stringValue){
        lock.lock();
        list.add(stringValue);
        condition.signalAll();
        lock.lock();
        concurrencySemaphore.release();
    }
}

Published 475 original articles · Like 16 · Visits 30,000+

Guess you like

Origin blog.csdn.net/qq_32370913/article/details/105576923