gpt4 book ai didi

java - 信号量公平参数不遵循先进先出

转载 作者:行者123 更新时间:2023-11-30 02:28:09 24 4
gpt4 key购买 nike

我正在开发一个简单的信号量程序,其中我正在初始化一个计数为 4 的信号量并启动 6 个线程。在 run 方法中,我正在获取信号量锁,并且在每个线程完成后,我将释放该锁。

这是我的代码:

import java.util.concurrent.Semaphore;

public class SemaphoreTest {

static Semaphore semaphore = new Semaphore(4, true);

static class MyThread extends Thread{

String name = "";

public MyThread(String name){
this.name = name;

}

public void run(){

System.out.println(name+" going to acquire lock...");
System.out.println("Available Permits = "+semaphore.availablePermits());

try {
semaphore.acquire();
System.out.println(name+" got permit.");

try{
for(int i=1;i<=1;i++){
System.out.println(name+" is performing operation "+i+". Available Semaphore permits are : "+semaphore.availablePermits());
Thread.sleep(1000);
}

}finally{
System.out.println(name+" Releasing lock...");
semaphore.release();
System.out.println("Available permits after releasing "+"name"+" = "+semaphore.availablePermits());
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}
}

public static void main(String[] args){
Thread t1 = new MyThread("A");
t1.start();

Thread t2 = new MyThread("B");
t2.start();

Thread t3 = new MyThread("C");
t3.start();

Thread t4 = new MyThread("D");
t4.start();

Thread t5 = new MyThread("E");
t5.start();

Thread t6 = new MyThread("F");
t6.start();
}

}

结果如下:

A going to acquire lock...
Available Permits = 4
C going to acquire lock...
A got permit.
A is performing operation 1. Available Semaphore permits are : 3
B going to acquire lock...
Available Permits = 3
B got permit.
F going to acquire lock...
E going to acquire lock...
Available Permits = 2
Available Permits = 3
D going to acquire lock...
Available Permits = 0
C got permit.
C is performing operation 1. Available Semaphore permits are : 0
E got permit.
E is performing operation 1. Available Semaphore permits are : 0
Available Permits = 2
B is performing operation 1. Available Semaphore permits are : 2
A Releasing lock...
E Releasing lock...
Available permits after releasing name = 2
D got permit.
D is performing operation 1. Available Semaphore permits are : 1
B Releasing lock...
C Releasing lock...
Available permits after releasing name = 1
F got permit.
F is performing operation 1. Available Semaphore permits are : 2
Available permits after releasing name = 2
Available permits after releasing name = 2
D Releasing lock...
F Releasing lock...
Available permits after releasing name = 3
Available permits after releasing name = 4

现在 Java 文档:

java.util.concurrent.Semaphore.Semaphore(int permits, boolean fair)

Creates a Semaphore with the given number of permits and the given fairness setting.

Parameters:
permits the initial number of permits available. This value may be negative, in which case releases must occur before any acquires will be granted.
fair true if this semaphore will guarantee first-in first-out granting of permits under contention, else false

构造函数 Semaphore(int allowed, boolean fair) 保证先进先出。但从该程序的输出来看,情况并不相同。锁的获取方式如下:

A -> B -> C -> E

锁的释放如下:

A -> E -> B -> C

请问是否符合预期?或者我缺少什么?

最佳答案

许可证释放的顺序只是 run 方法所花费时间的结果,与公平性无关。

这里的 FIFO 意味着,如果两个线程调用 semaphore.acquire() 并且没有可用的许可,则当许 cocoa 用时,第一个调用它的线程将是第一个接收许可的线程。

在您的示例中,A、B、C、E 获得许可,因为它们首先调用 acquire - 而 D 和 F 必须等待许 cocoa 用。那么看来 D 在 F 之前调用 acquire,因此获得了第一个可用的许可。

关于java - 信号量公平参数不遵循先进先出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45031767/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com