JUC并发工具类--Semaphore(信号量)

JUC并发工具类--Semaphore

  • 简介
  • 常用API
    • 构造方法
      • 参数:permits
      • 参数:fair
    • acquire
      • 参数:permits
    • acquireUninterruptibly()
      • 参数:permits
    • tryAcquire()
      • 参数:permits
      • 参数:timeout
      • 参数:unit
    • release()
      • 参数:permits
    • availablePermits()
  • 使用场景
    • 资源池
    • 限流

简介

Semaphore(信号量)是一种用于多线程编程的同步工具,用于控制同时访问某个资源的线程数量

JUC并发工具类--Semaphore(信号量)_第1张图片
Semaphore维护了一个计数器,线程可以通过调用acquire()方法来获取Semaphore中的许可证,当计数器为0时,调用acquire()的线程将被阻塞,直到有其他线程释放许可证;线程可以通过调用release()方法来释放Semaphore中的许可证,这会使Semaphore中的计数器增加,从而允许被阻塞或其他的线程访问共享资源。

常用API

构造方法

    /**
     * Creates a {@code Semaphore} with the given number of
     * permits and nonfair fairness setting.
     *
     * @param permits the initial number of permits available.
     *        This value may be negative, in which case releases
     *        must occur before any acquires will be granted.
     */
    public Semaphore(int permits) {
        sync = new NonfairSync(permits);
    }

    /**
     * Creates a {@code Semaphore} with the given number of
     * permits and the given fairness setting.
     *
     * @param permits the initial number of permits available.
     *        This value may be negative, in which case releases
     *        must occur before any acquires will be granted.
     * @param fair {@code true} if this semaphore will guarantee
     *        first-in first-out granting of permits under contention,
     *        else {@code false}
     */
    public Semaphore(int permits, boolean fair) {
        sync = fair ? new FairSync(permits) : new NonfairSync(permits);
    }

参数:permits

表示许可证的数量(资源数)。

参数:fair

是否指定锁的公平性。公平锁是指按照申请锁的顺序获取锁,非公平锁是指当所资源被释放时,正在申请此资源的多个线程争抢锁,谁抢到归谁。

acquire

阻塞并获取许可。通过参数permits,指定一次获取许可的数量。

    /**
     * Acquires a permit from this semaphore, blocking until one is
     * available, or the thread is {@linkplain Thread#interrupt interrupted}.
     *
     * 

Acquires a permit, if one is available and returns immediately, * reducing the number of available permits by one. * *

If no permit is available then the current thread becomes * disabled for thread scheduling purposes and lies dormant until * one of two things happens: *

    *
  • Some other thread invokes the {@link #release} method for this * semaphore and the current thread is next to be assigned a permit; or *
  • Some other thread {@linkplain Thread#interrupt interrupts} * the current thread. *
* *

If the current thread: *

    *
  • has its interrupted status set on entry to this method; or *
  • is {@linkplain Thread#interrupt interrupted} while waiting * for a permit, *
* then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared. * * @throws InterruptedException if the current thread is interrupted */
public void acquire() throws InterruptedException { sync.acquireSharedInterruptibly(1); } /** * Acquires the given number of permits from this semaphore, * blocking until all are available, * or the thread is {@linkplain Thread#interrupt interrupted}. * *

Acquires the given number of permits, if they are available, * and returns immediately, reducing the number of available permits * by the given amount. * *

If insufficient permits are available then the current thread becomes * disabled for thread scheduling purposes and lies dormant until * one of two things happens: *

    *
  • Some other thread invokes one of the {@link #release() release} * methods for this semaphore, the current thread is next to be assigned * permits and the number of available permits satisfies this request; or *
  • Some other thread {@linkplain Thread#interrupt interrupts} * the current thread. *
* *

If the current thread: *

    *
  • has its interrupted status set on entry to this method; or *
  • is {@linkplain Thread#interrupt interrupted} while waiting * for a permit, *
* then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared. * Any permits that were to be assigned to this thread are instead * assigned to other threads trying to acquire permits, as if * permits had been made available by a call to {@link #release()}. * * @param permits the number of permits to acquire * @throws InterruptedException if the current thread is interrupted * @throws IllegalArgumentException if {@code permits} is negative */
public void acquire(int permits) throws InterruptedException { if (permits < 0) throw new IllegalArgumentException(); sync.acquireSharedInterruptibly(permits); }

参数:permits

一次获取许可证的数量。

acquireUninterruptibly()

获取一个令牌,在获取成功之前,当前线程会被阻塞(中断被忽略)。通过参数permits,指定一次获取许可的数量。

    /**
     * Acquires a permit from this semaphore, blocking until one is
     * available.
     *
     * 

Acquires a permit, if one is available and returns immediately, * reducing the number of available permits by one. * *

If no permit is available then the current thread becomes * disabled for thread scheduling purposes and lies dormant until * some other thread invokes the {@link #release} method for this * semaphore and the current thread is next to be assigned a permit. * *

If the current thread is {@linkplain Thread#interrupt interrupted} * while waiting for a permit then it will continue to wait, but the * time at which the thread is assigned a permit may change compared to * the time it would have received the permit had no interruption * occurred. When the thread does return from this method its interrupt * status will be set. */ public void acquireUninterruptibly() { sync.acquireShared(1); } /** * Acquires the given number of permits from this semaphore, * blocking until all are available. * *

Acquires the given number of permits, if they are available, * and returns immediately, reducing the number of available permits * by the given amount. * *

If insufficient permits are available then the current thread becomes * disabled for thread scheduling purposes and lies dormant until * some other thread invokes one of the {@link #release() release} * methods for this semaphore, the current thread is next to be assigned * permits and the number of available permits satisfies this request. * *

If the current thread is {@linkplain Thread#interrupt interrupted} * while waiting for permits then it will continue to wait and its * position in the queue is not affected. When the thread does return * from this method its interrupt status will be set. * * @param permits the number of permits to acquire * @throws IllegalArgumentException if {@code permits} is negative */ public void acquireUninterruptibly(int permits) { if (permits < 0) throw new IllegalArgumentException(); sync.acquireShared(permits); }

参数:permits

一次获取许可证的数量。

tryAcquire()

尝试获取许可,通过参数permits,指定一次获取许可的数量。如果不指定参数timeout、unit则为非阻塞,指定参数timeout、unit时,会在指定时间内进行阻塞。

    /**
     * Acquires a permit from this semaphore, only if one is available at the
     * time of invocation.
     *
     * 

Acquires a permit, if one is available and returns immediately, * with the value {@code true}, * reducing the number of available permits by one. * *

If no permit is available then this method will return * immediately with the value {@code false}. * *

Even when this semaphore has been set to use a * fair ordering policy, a call to {@code tryAcquire()} will * immediately acquire a permit if one is available, whether or not * other threads are currently waiting. * This quot;barging" behavior can be useful in certain * circumstances, even though it breaks fairness. If you want to honor * the fairness setting, then use * {@link #tryAcquire(long, TimeUnit) tryAcquire(0, TimeUnit.SECONDS) } * which is almost equivalent (it also detects interruption). * * @return {@code true} if a permit was acquired and {@code false} * otherwise */ public boolean tryAcquire() { return sync.nonfairTryAcquireShared(1) >= 0; } /** * Acquires the given number of permits from this semaphore, only * if all are available at the time of invocation. * *

Acquires the given number of permits, if they are available, and * returns immediately, with the value {@code true}, * reducing the number of available permits by the given amount. * *

If insufficient permits are available then this method will return * immediately with the value {@code false} and the number of available * permits is unchanged. * *

Even when this semaphore has been set to use a fair ordering * policy, a call to {@code tryAcquire} will * immediately acquire a permit if one is available, whether or * not other threads are currently waiting. This * "barging" behavior can be useful in certain * circumstances, even though it breaks fairness. If you want to * honor the fairness setting, then use {@link #tryAcquire(int, * long, TimeUnit) tryAcquire(permits, 0, TimeUnit.SECONDS) } * which is almost equivalent (it also detects interruption). * * @param permits the number of permits to acquire * @return {@code true} if the permits were acquired and * {@code false} otherwise * @throws IllegalArgumentException if {@code permits} is negative */ public boolean tryAcquire(int permits) { if (permits < 0) throw new IllegalArgumentException(); return sync.nonfairTryAcquireShared(permits) >= 0; } /** * Acquires a permit from this semaphore, if one becomes available * within the given waiting time and the current thread has not * been {@linkplain Thread#interrupt interrupted}. * *

Acquires a permit, if one is available and returns immediately, * with the value {@code true}, * reducing the number of available permits by one. * *

If no permit is available then the current thread becomes * disabled for thread scheduling purposes and lies dormant until * one of three things happens: *

    *
  • Some other thread invokes the {@link #release} method for this * semaphore and the current thread is next to be assigned a permit; or *
  • Some other thread {@linkplain Thread#interrupt interrupts} * the current thread; or *
  • The specified waiting time elapses. *
* *

If a permit is acquired then the value {@code true} is returned. * *

If the current thread: *

    *
  • has its interrupted status set on entry to this method; or *
  • is {@linkplain Thread#interrupt interrupted} while waiting * to acquire a permit, *
* then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared. * *

If the specified waiting time elapses then the value {@code false} * is returned. If the time is less than or equal to zero, the method * will not wait at all. * * @param timeout the maximum time to wait for a permit * @param unit the time unit of the {@code timeout} argument * @return {@code true} if a permit was acquired and {@code false} * if the waiting time elapsed before a permit was acquired * @throws InterruptedException if the current thread is interrupted */ public boolean tryAcquire(long timeout, TimeUnit unit) throws InterruptedException { return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout)); } /** * Acquires the given number of permits from this semaphore, if all * become available within the given waiting time and the current * thread has not been {@linkplain Thread#interrupt interrupted}. * *

Acquires the given number of permits, if they are available and * returns immediately, with the value {@code true}, * reducing the number of available permits by the given amount. * *

If insufficient permits are available then * the current thread becomes disabled for thread scheduling * purposes and lies dormant until one of three things happens: *

    *
  • Some other thread invokes one of the {@link #release() release} * methods for this semaphore, the current thread is next to be assigned * permits and the number of available permits satisfies this request; or *
  • Some other thread {@linkplain Thread#interrupt interrupts} * the current thread; or *
  • The specified waiting time elapses. *
* *

If the permits are acquired then the value {@code true} is returned. * *

If the current thread: *

    *
  • has its interrupted status set on entry to this method; or *
  • is {@linkplain Thread#interrupt interrupted} while waiting * to acquire the permits, *
* then {@link InterruptedException} is thrown and the current thread's * interrupted status is cleared. * Any permits that were to be assigned to this thread, are instead * assigned to other threads trying to acquire permits, as if * the permits had been made available by a call to {@link #release()}. * *

If the specified waiting time elapses then the value {@code false} * is returned. If the time is less than or equal to zero, the method * will not wait at all. Any permits that were to be assigned to this * thread, are instead assigned to other threads trying to acquire * permits, as if the permits had been made available by a call to * {@link #release()}. * * @param permits the number of permits to acquire * @param timeout the maximum time to wait for the permits * @param unit the time unit of the {@code timeout} argument * @return {@code true} if all permits were acquired and {@code false} * if the waiting time elapsed before all permits were acquired * @throws InterruptedException if the current thread is interrupted * @throws IllegalArgumentException if {@code permits} is negative */ public boolean tryAcquire(int permits, long timeout, TimeUnit unit) throws InterruptedException { if (permits < 0) throw new IllegalArgumentException(); return sync.tryAcquireSharedNanos(permits, unit.toNanos(timeout)); }

参数:permits

一次获取许可证的数量。

参数:timeout

阻塞等待时间。

参数:unit

阻塞等待时间单位。

release()

释放许可。通过参数permits,指定一次释放许可的数量。

    /**
     * Releases a permit, returning it to the semaphore.
     *
     * 

Releases a permit, increasing the number of available permits by * one. If any threads are trying to acquire a permit, then one is * selected and given the permit that was just released. That thread * is (re)enabled for thread scheduling purposes. * *

There is no requirement that a thread that releases a permit must * have acquired that permit by calling {@link #acquire}. * Correct usage of a semaphore is established by programming convention * in the application. */ public void release() { sync.releaseShared(1); } /** * Releases the given number of permits, returning them to the semaphore. * *

Releases the given number of permits, increasing the number of * available permits by that amount. * If any threads are trying to acquire permits, then one * is selected and given the permits that were just released. * If the number of available permits satisfies that thread's request * then that thread is (re)enabled for thread scheduling purposes; * otherwise the thread will wait until sufficient permits are available. * If there are still permits available * after this thread's request has been satisfied, then those permits * are assigned in turn to other threads trying to acquire permits. * *

There is no requirement that a thread that releases a permit must * have acquired that permit by calling {@link Semaphore#acquire acquire}. * Correct usage of a semaphore is established by programming convention * in the application. * * @param permits the number of permits to release * @throws IllegalArgumentException if {@code permits} is negative */ public void release(int permits) { if (permits < 0) throw new IllegalArgumentException(); sync.releaseShared(permits); }

参数:permits

一次释放许可证的数量。

availablePermits()

当前可用的许可。

    /**
     * Returns the current number of permits available in this semaphore.
     *
     * 

This method is typically used for debugging and testing purposes. * * @return the number of permits available in this semaphore */ public int availablePermits() { return sync.getPermits(); }

使用场景

资源池

Semaphore可以用于实现资源池,以维护一组有限的共享资源。

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

/**
 * 数据库连接池实现
 */
public class SemaphoreDemo2 {

    final static ExecutorService executorService = Executors.newCachedThreadPool();

    public static void main(String[] args) {
        final ConnectPool pool = new ConnectPool(2);

        //5个线程并发来争抢连接资源
        for (int i = 0; i < 5; i++) {
            final int id = i + 1;
            executorService.execute(new Runnable() {
                @Override
                public void run() {
                    Connect connect = null;
                    try {
                        System.out.println("线程" + id + "等待获取数据库连接");
                        connect = pool.openConnect();
                        System.out.println("线程" + id + "已拿到数据库连接:" + connect);
                        //进行数据库操作2秒...然后释放连接
                        Thread.sleep(2000);
                        System.out.println("线程" + id + "释放数据库连接:" + connect);

                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } finally {
                        pool.releaseConnect(connect);
                    }

                }
            });
        }
    }
}

//数据库连接池
class ConnectPool {
    private int size;
    private Connect[] connects;

    //记录对应下标的Connect是否已被使用
    private boolean[] connectFlag;
    //信号量对象
    private Semaphore semaphore;

    /**
     * size:初始化连接池大小
     */
    public ConnectPool(int size) {
        this.size = size;
        semaphore = new Semaphore(size, true);
        connects = new Connect[size];
        connectFlag = new boolean[size];
        initConnects();//初始化连接池
    }

    private void initConnects() {
        for (int i = 0; i < this.size; i++) {
            connects[i] = new Connect();
        }
    }

    /**
     * 获取数据库连接
     *
     * @return
     * @throws InterruptedException
     */
    public Connect openConnect() throws InterruptedException {
        //得先获得使用许可证,如果信号量为0,则拿不到许可证,一直阻塞直到能获得
        semaphore.acquire();
        return getConnect();
    }

    private synchronized Connect getConnect() {
        for (int i = 0; i < connectFlag.length; i++) {
            if (!connectFlag[i]) {
                //标记该连接已被使用
                connectFlag[i] = true;
                return connects[i];
            }
        }
        return null;
    }

    /**
     * 释放某个数据库连接
     */
    public synchronized void releaseConnect(Connect connect) {
        for (int i = 0; i < this.size; i++) {
            if (connect == connects[i]) {
                connectFlag[i] = false;
                semaphore.release();
            }
        }
    }
}

/**
 * 数据库连接
 */
class Connect {

    private static int count = 1;
    private int id = count++;

    public Connect() {
        //假设打开一个连接很耗费资源,需要等待1秒
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("连接#" + id + "#已与数据库建立通道!");
    }

    @Override
    public String toString() {
        return "#" + id + "#";

    }

}

限流

Semaphore可以用于限制对共享资源的并发访问数量,以控制系统的流量。

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * 模拟限流场景
 */
public class SemaphoreDemo {

    /**
     * 同一时刻最多只允许有两个并发
     */
    private static Semaphore semaphore = new Semaphore(5, true);

    private static Executor executor = Executors.newFixedThreadPool(6);

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            executor.execute(() -> getProductInfo2());
        }
    }

    public static String getProductInfo() {
        try {
            //申请许可
            semaphore.acquire();
            System.out.println("请求服务");
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            //释放许可
            semaphore.release();
        }
        return "返回商品详情信息";
    }

    public static String getProductInfo2() {

        if (!semaphore.tryAcquire()) {
            System.out.println(Thread.currentThread().getName() + "请求被流控了");
            return "请求被流控了";
        }
        try {
            System.out.println(Thread.currentThread().getName() + "请求服务");
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            semaphore.release();
        }
        return "返回商品详情信息";
    }
}

你可能感兴趣的:(#,JUC,java)