2013-05-30 27 views
4

看一些文献的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

据我了解,是的,你的想法是正确的,因为公平旗语使用FairSync,而且它的获取机制不上可用的许可数量接力,但仅限于在线程队列中的第一个线程:

 protected int tryAcquireShared(int acquires) { 
     for (;;) { 
      if (getFirstQueuedThread() != Thread.currentThread() && 
       hasQueuedThreads()) 
       return -1; 
     .... 
+0

+1指向我确认它的实现 – betomontejo

0

我会说,因为他们都在其构造函数中调用start,所以只能保证创建线程的顺序及其启动顺序,您不能说任何关于它们的方法被调用的顺序。

所以 - 从根本上说 - 你是对的,信号量的数量根本没有在顺序中发挥作用。