private static void work(Phaser phaser) {
int phase = phaser.getPhase();
phaser.arriveAndAwaitAdvance();
System.out.println("--- Phase – " + phase + " is terminated ---");
} }
class Worker implements Runnable {
private static final Random rand = new Random();
private Phaser phaser;
private double value = 0;
Maersk.com/Mitas
�e Graduate Programme for Engineers and Geoscientists
I wanted real responsibili�
I joined MITAS because
Maersk.com/Mitas�e Graduate Programme for Engineers and Geoscientists
I wanted real responsibili�
I joined MITAS because
Maersk.com/Mitas
�e Graduate Programme for Engineers and Geoscientists
I wanted real responsibili�
I joined MITAS because
Maersk.com/Mitas
�e Graduate Programme for Engineers and Geoscientists
I wanted real responsibili�
I joined MITAS because
www.discovermitas.com
JAVA 8:MULTITHREADED PROGRAMS
76
ConCurrenCy tools JAVA 8:MULTITHREADED PROGRAMS
76
COnCURREnCy TOOLS
public Worker(Phaser phaser) {
this.phaser = phaser;
this.phaser.register();
System.out.println("New thred registered: " + Thread.currentThread().getName());
}
public void run() {
todo(2);
todo(3);
todo(5);
result();
phaser.arriveAndDeregister();
}
private void todo(int t) {
System.out.println(Thread.currentThread().getName() +
" – has reached the barrier and works in phase " + phaser.getPhase() + ", Value = " + value);
phaser.arriveAndAwaitAdvance();
work(rand.nextInt(Integer.MAX_VALUE), t);
}
private void result() {
System.out.println(Thread.currentThread().getName() +
" – has reached the barrier and works in phase " + phaser.getPhase() + ", Value = " + value);
phaser.arriveAndAwaitAdvance();
}
private void delay(int time) {
try {
Thread.sleep(time);
}
catch (InterruptedException e) {
} }
JAVA 8:MULTITHREADED PROGRAMS ConCurrenCy tools
JAVA 8:MULTITHREADED PROGRAMS COnCURREnCy TOOLS
private void work(long n, int t) {
for (int i = 0; i < n; ++i) value = Math.sqrt(t);
} }
The class Worker defines the secondary threads. The constructor has a Phaser object as a parameter, and the constructor sign up to the Phaser object as one of the threads which the object has to wait for:
this.phaser.register();
The run() method executes four methods (calls the method todo() three times and the method result() once). Each of these methods carry out some work in a phase. That is that the first todo() is first performed when all threads have reached the first barrier, the next todo() when all threads have reached the next barrier and so on. If you look at each phase’s methods, it is not so important, what they do, but they perform the method
phaser.arriveAndAwaitAdvance();
which means that the thread must wait at the barrier until all threads have reached. The Phaser object is created in the main() method:
Phaser phaser = new Phaser(1);
where there is registered a single thread, which is the primary thread. The primary thread creates then 5 other threads and start them and then perform the method work(). The important thing here is the statement
phaser.arriveAndAwaitAdvance();
which means that all threads incl. the primary thread will have to wait at a barrier until all the other threads arrives. If the program is performed the result is:
New thred registered: main New thred registered: main New thred registered: main New thred registered: main New thred registered: main
The class Worker defines the secondary threads. The constructor has a Phaser object as a parameter, and the constructor sign up to the Phaser object as one of the threads which the object has to wait for:
JAVA 8:MULTITHREADED PROGRAMS COnCURREnCy TOOLS
private void work(long n, int t) {
for (int i = 0; i < n; ++i) value = Math.sqrt(t);
} }
The class Worker defines the secondary threads. The constructor has a Phaser object as a parameter, and the constructor sign up to the Phaser object as one of the threads which the object has to wait for:
this.phaser.register();
The run() method executes four methods (calls the method todo() three times and the method result() once). Each of these methods carry out some work in a phase. That is that the first todo() is first performed when all threads have reached the first barrier, the next todo() when all threads have reached the next barrier and so on. If you look at each phase’s methods, it is not so important, what they do, but they perform the method
phaser.arriveAndAwaitAdvance();
which means that the thread must wait at the barrier until all threads have reached. The Phaser object is created in the main() method:
Phaser phaser = new Phaser(1);
where there is registered a single thread, which is the primary thread. The primary thread creates then 5 other threads and start them and then perform the method work(). The important thing here is the statement
phaser.arriveAndAwaitAdvance();
which means that all threads incl. the primary thread will have to wait at a barrier until all the other threads arrives. If the program is performed the result is:
New thred registered: main New thred registered: main New thred registered: main New thred registered: main New thred registered: main
The run() method executes four methods (calls the method todo() three times and the method result() once). Each of these methods carry out some work in a phase. That is that the first todo() is first performed when all threads have reached the first barrier, the next todo() when all threads have reached the next barrier and so on. If you look at each phase’s methods, it is not so important, what they do, but they perform the method
JAVA 8:MULTITHREADED PROGRAMS COnCURREnCy TOOLS
private void work(long n, int t) {
for (int i = 0; i < n; ++i) value = Math.sqrt(t);
} }
The class Worker defines the secondary threads. The constructor has a Phaser object as a parameter, and the constructor sign up to the Phaser object as one of the threads which the object has to wait for:
this.phaser.register();
The run() method executes four methods (calls the method todo() three times and the method result() once). Each of these methods carry out some work in a phase. That is that the first todo() is first performed when all threads have reached the first barrier, the next todo() when all threads have reached the next barrier and so on. If you look at each phase’s methods, it is not so important, what they do, but they perform the method
phaser.arriveAndAwaitAdvance();
which means that the thread must wait at the barrier until all threads have reached. The Phaser object is created in the main() method:
Phaser phaser = new Phaser(1);
where there is registered a single thread, which is the primary thread. The primary thread creates then 5 other threads and start them and then perform the method work(). The important thing here is the statement
phaser.arriveAndAwaitAdvance();
which means that all threads incl. the primary thread will have to wait at a barrier until all the other threads arrives. If the program is performed the result is:
New thred registered: main New thred registered: main New thred registered: main New thred registered: main New thred registered: main
which means that the thread must wait at the barrier until all threads have reached. The Phaser object is created in the main() method:
JAVA 8:MULTITHREADED PROGRAMS COnCURREnCy TOOLS
private void work(long n, int t) {
for (int i = 0; i < n; ++i) value = Math.sqrt(t);
} }
The class Worker defines the secondary threads. The constructor has a Phaser object as a parameter, and the constructor sign up to the Phaser object as one of the threads which the object has to wait for:
this.phaser.register();
The run() method executes four methods (calls the method todo() three times and the method result() once). Each of these methods carry out some work in a phase. That is that the first todo() is first performed when all threads have reached the first barrier, the next todo() when all threads have reached the next barrier and so on. If you look at each phase’s methods, it is not so important, what they do, but they perform the method
phaser.arriveAndAwaitAdvance();
which means that the thread must wait at the barrier until all threads have reached. The Phaser object is created in the main() method:
Phaser phaser = new Phaser(1);
where there is registered a single thread, which is the primary thread. The primary thread creates then 5 other threads and start them and then perform the method work(). The important thing here is the statement
phaser.arriveAndAwaitAdvance();
which means that all threads incl. the primary thread will have to wait at a barrier until all the other threads arrives. If the program is performed the result is:
New thred registered: main New thred registered: main New thred registered: main New thred registered: main New thred registered: main
where there is registered a single thread, which is the primary thread. The primary thread creates then 5 other threads and start them and then perform the method work(). The important thing here is the statement
JAVA 8:MULTITHREADED PROGRAMS COnCURREnCy TOOLS
private void work(long n, int t) {
for (int i = 0; i < n; ++i) value = Math.sqrt(t);
} }
The class Worker defines the secondary threads. The constructor has a Phaser object as a parameter, and the constructor sign up to the Phaser object as one of the threads which the object has to wait for:
this.phaser.register();
The run() method executes four methods (calls the method todo() three times and the method result() once). Each of these methods carry out some work in a phase. That is that the first todo() is first performed when all threads have reached the first barrier, the next todo() when all threads have reached the next barrier and so on. If you look at each phase’s methods, it is not so important, what they do, but they perform the method
phaser.arriveAndAwaitAdvance();
which means that the thread must wait at the barrier until all threads have reached. The Phaser object is created in the main() method:
Phaser phaser = new Phaser(1);
where there is registered a single thread, which is the primary thread. The primary thread creates then 5 other threads and start them and then perform the method work(). The important thing here is the statement
phaser.arriveAndAwaitAdvance();
which means that all threads incl. the primary thread will have to wait at a barrier until all the other threads arrives. If the program is performed the result is:
New thred registered: main New thred registered: main New thred registered: main New thred registered: main New thred registered: main
which means that all threads incl. the primary thread will have to wait at a barrier until all the other threads arrives. If the program is performed the result is:
JAVA 8:MULTITHREADED PROGRAMS COnCURREnCy TOOLS
private void work(long n, int t) {
for (int i = 0; i < n; ++i) value = Math.sqrt(t);
} }
The class Worker defines the secondary threads. The constructor has a Phaser object as a parameter, and the constructor sign up to the Phaser object as one of the threads which the object has to wait for:
this.phaser.register();
The run() method executes four methods (calls the method todo() three times and the method result() once). Each of these methods carry out some work in a phase. That is that the first todo() is first performed when all threads have reached the first barrier, the next todo() when all threads have reached the next barrier and so on. If you look at each phase’s methods, it is not so important, what they do, but they perform the method
phaser.arriveAndAwaitAdvance();
which means that the thread must wait at the barrier until all threads have reached. The Phaser object is created in the main() method:
Phaser phaser = new Phaser(1);
where there is registered a single thread, which is the primary thread. The primary thread creates then 5 other threads and start them and then perform the method work(). The important thing here is the statement
phaser.arriveAndAwaitAdvance();
which means that all threads incl. the primary thread will have to wait at a barrier until all the other threads arrives. If the program is performed the result is:
New thred registered: main New thred registered: main New thred registered: main New thred registered: main New thred registered: main
JAVA 8:MULTITHREADED PROGRAMS
78
ConCurrenCy tools
78 JAVA 8:MULTITHREADED PROGRAMS
78
COnCURREnCy TOOLS
78
--- Start Phaser
Thread-2 – has reached the barrier and works in phase 0, Value = 0.0 Thread-1 – has reached the barrier and works in phase 0, Value = 0.0 Thread-4 – has reached the barrier and works in phase 0, Value = 0.0 Thread-3 – has reached the barrier and works in phase 0, Value = 0.0 Thread-5 – has reached the barrier and works in phase 0, Value = 0.0 --- Phase – 0 is terminated
Thread-2 – has reached the barrier and works in phase 1, Value = 1.4142135623730951 Thread-5 – has reached the barrier and works in phase 1, Value = 1.4142135623730951 Thread-4 – has reached the barrier and works in phase 1, Value = 1.4142135623730951 Thread-3 – has reached the barrier and works in phase 1, Value = 1.4142135623730951 Thread-1 – has reached the barrier and works in phase 1, Value = 1.4142135623730951 --- Phase – 1 is terminated
Thread-4 – has reached the barrier and works in phase 2, Value = 1.7320508075688772 Thread-1 – has reached the barrier and works in phase 2, Value = 1.7320508075688772 Thread-5 – has reached the barrier and works in phase 2, Value = 1.7320508075688772 Thread-3 – has reached the barrier and works in phase 2, Value = 1.7320508075688772 Thread-2 – has reached the barrier and works in phase 2, Value = 1.7320508075688772 --- Phase – 2 is terminated
Thread-2 – has reached the barrier and works in phase 3, Value = 2.23606797749979 Thread-1 – has reached the barrier and works in phase 3, Value = 2.23606797749979 Thread-3 – has reached the barrier and works in phase 3, Value = 2.23606797749979 Thread-5 – has reached the barrier and works in phase 3, Value = 2.23606797749979 Thread-4 – has reached the barrier and works in phase 3, Value = 2.23606797749979