Home > Java > javaTutorial > How to use Semaphore to implement current limiting in Java?

How to use Semaphore to implement current limiting in Java?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-04-21 20:34:06
forward
1541 people have browsed it

Concept

1. Semaphore can be regarded as a semaphore that has been widely translated into a semaphore. Conceptually, a semaphore maintains a set of credentials, and the thread that obtains the credentials can Access resources and release them after use. We can use semaphores to limit concurrent threads accessing specific resources.

2. It can be simply summarized as: a counter, a waiting queue, and three methods. In the semaphore model, counters and waiting queues are transparent and can only be accessed through the three methods provided by the semaphore model, namely Internet, acquire and release.

Example

public class SemaphoreDemo {
 
    static class Link {
    }
 
    static class ObjPool<T, R> {
 
        final List<T> pool;
 
        final Semaphore semaphore;
 
        ObjPool(int size, T t) {
            pool = new Vector<>(size);
            for (int i = 0; i < size; i++) {
                pool.add(t);
            }
            semaphore = new Semaphore(size);
        }
 
        public R exec(Function<T, R> func) throws Exception {
            T t = null;
            semaphore.acquire();
            try {
                System.out.println(Thread.currentThread().getName() + "---------争夺锁--------");
 
                t = pool.remove(0);
                System.out.println(Thread.currentThread().getName() + " 拿到锁执行");
                return func.apply(t);
            } finally {
                pool.add(t);
                semaphore.release();
            }
        }
    }
 
    public static void main(String[] args) {
        ObjPool objPool = new ObjPool(5, new Link());
        for (int i = 0; i < 30; i++) {
            new Thread(() -> {
                try {
                    objPool.exec(t -> t.toString());
                } catch (Exception e) {
                }
            }).start();
        }
    }
}
Copy after login

The above is the detailed content of How to use Semaphore to implement current limiting in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template