java并发:线程同步机制之Semaphore

Semaphore是一个用于控制并发访问共享资源的工具,它维护一个许可集合。在Java中,Semaphore限制了同一时间访问特定资源的线程数量。例如,一个服务器资源有限,允许同时3个用户访问。Semaphore通过acquire()获取许可,release()释放许可,确保不超过预设的并发访问数。当许可用尽时,其他线程会等待。示例代码展示了如何使用Semaphore来实现这一功能。
摘要由CSDN通过智能技术生成

一、初识vb.net教程Semaphore

小结:

A、可以将信号量可视c#教程化为一个计数器,它可以递增或递减。

B、从概念上讲,信号量python基础教程维护了一个许可集合,Semaphore对可用的许可进行计数。

C、当计数器的值为0时,它能够java基础教程使线程等待。

二、示例

The three steps you must follow when you use a semaphore to implement a critical section and protect the access to a shared resource:

  • First, you acquire the semaphore, with the acquire() method.
  • Then, you do the necessary operations with the shared resource.
  • Finally, release the semaphore with the release() method.

场景:

假设一个服务器资源有限,任意sql教程某一时刻只允许3个人同时访问,这时一共来了10个人

复制代码

package com.test;

import java.util.concurrent.Semaphore;

public class SemaphoreDemo{
    
    public static void main(String args[]) throws Exception{
        
        final Semaphore semaphore = new Semaphore(3);//一次只允许3个人进行访问
        
        for(int i=0;i<10;i++) {
            final int no = i;

            Runnable thread = new Runnable() {
                public void run (){
                    try {
                        System.out.println("用户"+no+"连接上了:");
                        Thread.sleep(300L);
                        semaphore.acquire();//获取执行的许可
                        System.out.println("用户"+no+"开始访问后台程序...");
                        Thread.sleep(1000L);//模仿用户访问服务过程
                        semaphore.release();//释放,允许下一个线程访问后台
                        System.out.println("用户"+no+"访问结束。");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };

            new Thread(thread).start();
        }
        
        System.out.println("Main thread end!");
    }
}

复制代码

上述代码运行结果如下:

复制代码

用户1连接上了:
用户3连接上了:
用户4连接上了:
用户2连接上了:
用户0连接上了:
用户5连接上了:
用户7连接上了:
Main thread end!
用户6连接上了:
用户8连接上了:
用户9连接上了:
用户3开始访问后台程序...
用户4开始访问后台程序...
用户2开始访问后台程序...
用户4访问结束。
用户3访问结束。
用户7开始访问后台程序...
用户0开始访问后台程序...
用户8开始访问后台程序...
用户2访问结束。
用户5开始访问后台程序...
用户0访问结束。
用户7访问结束。
用户1开始访问后台程序...
用户8访问结束。
用户6开始访问后台程序...
用户1访问结束。
用户9开始访问后台程序...
用户5访问结束。
用户6访问结束。
用户9访问结束。

复制代码

从结果上可以看出来,10个人同时进来,但是只能同时3个人访问资源,释放一个允许进来一个

Note:

When a thread has finished the use of the shared resource, it must release the semaphore so that the other threads can access the shared resource.

That operation increases the internal counter of the semaphore.

三、详解Semaphore

四、参考资料

(1)https://howtodoinjava.com/java/multi-threading/binary-semaphore-tutorial-and-example/

(2)https://howtodoinjava.com/java/multi-threading/control-concurrent-access-to-multiple-copies-of-a-resource-using-semaphore/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值