移相器相位值转义

时间:2019-02-07 08:52:18

标签: java phaser

Given,这是一个具有main方法的类,该类初始化Phaser,并创建(说)3个线程以对同一Phaser上的相位进行计数。

public class PhaserDemo2 implements Runnable {

    private static Phaser CLASS_PHASER;
    private static int COUNTER;

    public static void main(String[] args) {
        COUNTER = 5;
        // create a Phaser and register main Thread
        CLASS_PHASER = new Phaser(1);
        System.out.println(Integer.MIN_VALUE);
        System.out.println("Phase value:" + CLASS_PHASER.getPhase() + ", Registered Parties:" 
                + CLASS_PHASER.getRegisteredParties() + ", and Counter:" + COUNTER);
        // create 3 Threads
        new Thread(new PhaserDemo2()).start();
        new Thread(new PhaserDemo2()).start();
        new Thread(new PhaserDemo2()).start();
        // de-register main Thread
        CLASS_PHASER.arriveAndDeregister();
    }

    @Override
    public void run() {
        // register the current Thread with Phaser
        CLASS_PHASER.register();
        // print details
        System.out.println("Phase value:" + CLASS_PHASER.getPhase() + ", Registered Parties:" 
                + CLASS_PHASER.getRegisteredParties() + ", and Counter:" + COUNTER);
        // wait till other Threads have printed as well
        CLASS_PHASER.arriveAndAwaitAdvance();
        // de-register this Thread
        CLASS_PHASER.arriveAndDeregister();
    }
}

该代码对于大多数执行均能正常工作,并产生如下所示的输出,

-2147483648
Phase value:0, Registered Parties:1, and Counter:5
Phase value:0, Registered Parties:2, and Counter:5
Phase value:1, Registered Parties:2, and Counter:5
Phase value:1, Registered Parties:3, and Counter:5

-2147483648
Phase value:0, Registered Parties:1, and Counter:5
Phase value:0, Registered Parties:2, and Counter:5
Phase value:0, Registered Parties:3, and Counter:5
Phase value:0, Registered Parties:3, and Counter:5

但是对于一些奇怪的执行,它会产生如下所示的输出

-2147483648
Phase value:0, Registered Parties:1, and Counter:5
Phase value:0, Registered Parties:2, and Counter:5
Phase value:0, Registered Parties:3, and Counter:5
Phase value:-2147483646, Registered Parties:0, and Counter:5

-2147483648
Phase value:0, Registered Parties:1, and Counter:5
Phase value:0, Registered Parties:2, and Counter:5
Phase value:-2147483646, Registered Parties:0, and Counter:5
Phase value:-2147483646, Registered Parties:0, and Counter:5

-2147483648
Phase value:0, Registered Parties:1, and Counter:5
Phase value:-2147483647, Registered Parties:0, and Counter:5
Phase value:-2147483647, Registered Parties:0, and Counter:5
Phase value:-2147483647, Registered Parties:0, and Counter:5

我不确定这是否可以成功复制,因为这种情况很少发生。否则,想知道导致此错误的原因。
声明:只是Phaser的初学者

1 个答案:

答案 0 :(得分:3)

当您获得负值作为相位值时-您的相位终止。

Java doc中摘录的.getPhase()方法:

  

返回当前相数。最大相位数为Integer.MAX_VALUE,之后它从零重新开始。终止后,阶段号为负,在这种情况下,可以通过getPhase()+ Integer.MIN_VALUE获得终止前的主要阶段。

那么,为什么会这样?

在同一docs终止部分中:

  

onAdvance的调用返回true时,将触发终止。如果取消注册导致注册方的数量变为零,则默认实现将返回true。

在您的情况下,有时注册方的数量变为0。例如,当您注册主线程时,有可能没有注册任何线程-您可以注销主线程。 这种情况是:

-2147483648
Phase value:0, Registered Parties:1, and Counter:5
Phase value:-2147483647, Registered Parties:0, and Counter:5
Phase value:-2147483647, Registered Parties:0, and Counter:5
Phase value:-2147483647, Registered Parties:0, and Counter:5

还有另一种情况。 Excerpt from baeldung

  

对到达AndAwaitAdvance()的调用将导致当前线程在屏障上等待。如前所述,当到达的参与者的数量与注册的参与者的数量相同时,将继续执行。

这种情况可能是:

-2147483648
Phase value:0, Registered Parties:1, and Counter:5
Phase value:0, Registered Parties:2, and Counter:5
Phase value:-2147483646, Registered Parties:0, and Counter:5
Phase value:-2147483646, Registered Parties:0, and Counter:5