Java Reference
In-Depth Information
System.out.println(taskName + ":Initialized...");
// Wait for all parties to arrive to start the task
phaser.arriveAndAwaitAdvance();
System.out.println(taskName + ":Started...");
}
}
Listing 6-37 has the code to test three tasks of StartTogetherTask type.
Listing 6-37. Testing Some Objects of the StartTogetherTask Class with a Phaser
// StartTogetherTaskTest.java
package com.jdojo.threads;
import java.util.concurrent.Phaser;
public class StartTogetherTaskTest {
public static void main(String[] args) {
// Start with 1 registered party
Phaser phaser = new Phaser(1);
// Let's start three tasks
final int TASK_COUNT = 3;
for(int i = 1; i <= TASK_COUNT; i++) {
// Register a new party with the phaser for each task
phaser.register();
// Now create the task and start it
String taskName = "Task #" + i;
StartTogetherTask task = new StartTogetherTask(taskName, phaser);
task.start();
}
// Now, deregister the self, so all tasks can advance
phaser.arriveAndDeregister();
}
}
Task #1:Initializing...
Task #2:Initializing...
Task #3:Initializing...
Task #2:Initialized...
Task #1:Initialized...
Task #3:Initialized...
Task #3:Started...
Task #1:Started...
Task #2:Started...
Search WWH ::




Custom Search