看一些文献的1Z0-804考试,我觉得它这个例子问题:信号量的大小能否与线程的执行顺序相关?
考虑下面的程序:
class ATMRoom {
public static void main(String []args) {
Semaphore machines = new Semaphore(2); //#1
new Person(machines, "Mickey");
new Person(machines, "Donald");
new Person(machines, "Tom");
new Person(machines, "Jerry");
new Person(machines, "Casper");
}
}
class Person extends Thread {
private Semaphore machines;
public Person(Semaphore machines, String name) {
this.machines = machines;
this.setName(name);
this.start();
}
public void run() {
try {
System.out.println(getName()
+ " waiting to access an ATM machine");
machines.acquire();
System.out.println(getName()
+ " is accessing an ATM machine");
Thread.sleep(1000);
System.out.println(getName()
+ " is done using the ATM machine");
machines.release();
} catch(InterruptedException ie) {
System.err.println(ie);
}
}
}
哪一个如果用以下语句替换语句#1,选项是否正确?
Semaphore machines = new Semaphore(2, true);
省略答案
正确答案的解释引起了我的注意:
第二个参数状态信号对象的公平策略。但是,还有 是信号量对象的两个许可证;所以你不能预测的订单,其中等待 的人将获得访问ATM的权限。
我要说的是一个无法预测的顺序,只是因为线程的不确定性特点,在信号量许可证的数量不因为和公平性参数保证了等待的线程在唤醒他们获得信号的顺序相同,但仍然无法确定获取顺序。我的解释是否正确?
+1指向我确认它的实现 – betomontejo