[Concurrent Programming] - Semaphore - Create String Pool

Get into the habit of writing together! This is the 9th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Create String Pool with Semaphore

The class Semaphore can effectively limit the number of threads that execute tasks concurrently. This function can be applied to the pool pool technology, and the number of threads that access the data in the pool pool at the same time can be set.

At the same time, several threads can access the data in the pool, but at the same time only one thread can obtain the data, and then put it back into the pool after use.

String pool implementation code:

public class ListPool {

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

    public ListPool(){
        super();
        for (int i = 0; i < poolMaxSize ; i++) {
            list.add("ozx"+(i+1));
        }
    }

    public String get(){
        String getValue = null;
        try {
            semaphore.acquire();
            reentrantLock.lock();
            while(list.size() == 0){
                condition.await();
            }
            getValue = list.remove(0);
            reentrantLock.unlock();
            semaphore.release();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return getValue;
    }

    public void put(String value){
        reentrantLock.lock();
        list.add(value);
        condition.signalAll();
        reentrantLock.unlock();
    }
}

复制代码

Thread class code:

@Slf4j
public class ListPoolThread implements Runnable {

    private ListPool listPool;

    public ListPoolThread(ListPool listPool){
        super();
        this.listPool=listPool;
    }


    @Override
    public void run() {
        for (int i = 0; i < Integer.MAX_VALUE ; i++) {
            String getValue = listPool.get();
            log.info("线程名:{} 获取的字符串的值:{}",Thread.currentThread().getName(),getValue);
            listPool.put(getValue);
        }
    }
}
复制代码

The running class code is as follows:

public class ListPoolTest {
    public static void main(String[] args) {
        ListPool listPool = new ListPool();
        ListPoolThread[] poolThreads = new ListPoolThread[10];
        for (int i = 0; i < poolThreads.length ; i++) {
            poolThreads[i] = new ListPoolThread(listPool);
        }
        for (int i = 0; i < poolThreads.length ; i++) {
            new Thread(poolThreads[i]).start();
        }
    }
}
复制代码

The results are as follows:

17:26:28.111 [Thread-5] INFO com.ozx.concurrentprogram.semaphore.entity.ListPoolThread - 线程名:Thread-5 获取的字符串的值:ozx1
17:26:28.111 [Thread-2] INFO com.ozx.concurrentprogram.semaphore.entity.ListPoolThread - 线程名:Thread-2 获取的字符串的值:ozx2
17:26:28.111 [Thread-8] INFO com.ozx.concurrentprogram.semaphore.entity.ListPoolThread - 线程名:Thread-8 获取的字符串的值:ozx5
17:26:28.111 [Thread-7] INFO com.ozx.concurrentprogram.semaphore.entity.ListPoolThread - 线程名:Thread-7 获取的字符串的值:ozx4
17:26:28.111 [Thread-5] INFO com.ozx.concurrentprogram.semaphore.entity.ListPoolThread - 线程名:Thread-5 获取的字符串的值:ozx1
17:26:28.113 [Thread-5] INFO com.ozx.concurrentprogram.semaphore.entity.ListPoolThread - 线程名:Thread-5 获取的字符串的值:ozx1
17:26:28.113 [Thread-5] INFO com.ozx.concurrentprogram.semaphore.entity.ListPoolThread - 线程名:Thread-5 获取的字符串的值:ozx1
复制代码

Guess you like

Origin juejin.im/post/7085258436048486414