Phaser 相位值转义

Phaser phase value escapes

给定一个 class 带有 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 的初学者

当你得到负值作为阶段值时 - 你的阶段终止。

摘自 Java doc.getPhase() 方法:

Returns the current phase number. The maximum phase number is Integer.MAX_VALUE, after which it restarts at zero. Upon termination, the phase number is negative, in which case the prevailing phase prior to termination may be obtained via getPhase() + Integer.MIN_VALUE.

那么,为什么会这样?

来自同一个 docs 终止部分:

Termination is triggered when an invocation of onAdvance returns true. The default implementation returns true if a deregistration has caused the number of registered parties to become zero.

在您的情况下,有时注册方的数量变为 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:

The call to the arriveAndAwaitAdvance() will cause the current thread to wait on the barrier. As already mentioned, when the number of arrived parties becomes the same as the number of registered parties, the execution will continue.

这种情况可能是:

-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