• No results found

RUN FASTER

In document Java 8 Multithreaded Programs (Page 72-75)

writeLock.release();

}

public void getReadLock() throws InterruptedException {

readLock.acquire();

}

public void releaseReadLock() {

readLock.release();

} }

As a next step you must write a Writer class that defines a Runnable type that can add names to the data structure Storage:

class Writer implements Runnable {

private String[] names;

As a next step you must write a Writer class that defines a Runnable type that can add names to the data structure Storage:

JAVA 8:MULTITHREADED PROGRAMS

72

COnCURREnCy TOOLS

72

public void releaseWriteLock() {

writeLock.release();

}

public void getReadLock() throws InterruptedException {

readLock.acquire();

}

public void releaseReadLock() {

readLock.release();

} }

As a next step you must write a Writer class that defines a Runnable type that can add names to the data structure Storage:

class Writer implements Runnable {

private String[] names;

EXPERIENCE THE POWER OF FULL ENGAGEMENT…

RUN FASTER.

RUN LONGER..

RUN EASIER…

READ MORE & PRE-ORDER TODAY WWW.GAITEYE.COM Challenge the way we run

1349906_A6_4+0.indd 1 22-08-2014 12:56:57

JAVA 8:MULTITHREADED PROGRAMS ConCurrenCy tools

JAVA 8:MULTITHREADED PROGRAMS COnCURREnCy TOOLS

public Writer(String[] names) {

// sleep a random time less than 2 seconds }

} }

You must corresponding write a class Reader that defines a Runnable type that can read the names in the data structure Storage:

class Reader implements Runnable {

// sleep a random time less than a ½ second }

} }

Then there is the only main program:

public class SemaphoreProgram {

private static final String[] boys = ... // array with 10 boy names private static final String[] girls = ... // array with 10 girl names

You must corresponding write a class Reader that defines a Runnable type that can read the names in the data structure Storage:

JAVA 8:MULTITHREADED PROGRAMS COnCURREnCy TOOLS

public Writer(String[] names) {

// sleep a random time less than 2 seconds }

} }

You must corresponding write a class Reader that defines a Runnable type that can read the names in the data structure Storage:

class Reader implements Runnable {

// sleep a random time less than a ½ second }

} }

Then there is the only main program:

public class SemaphoreProgram {

private static final String[] boys = ... // array with 10 boy names private static final String[] girls = ... // array with 10 girl names

Then there is the only main program:

JAVA 8:MULTITHREADED PROGRAMS COnCURREnCy TOOLS

public Writer(String[] names) {

// sleep a random time less than 2 seconds }

} }

You must corresponding write a class Reader that defines a Runnable type that can read the names in the data structure Storage:

class Reader implements Runnable {

// sleep a random time less than a ½ second }

} }

Then there is the only main program:

public class SemaphoreProgram {

private static final String[] boys = ... // array with 10 boy names private static final String[] girls = ... // array with 10 girl names

JAVA 8:MULTITHREADED PROGRAMS

public static void main(String[] args) {

// creates a thread to a Writer object which adds boy names // creates a thread to a Writer object which adds girl names // Creates 10 Reader threads, when it should be daemon threads // start all the threads

// the primary thread should join the two writer threds }

}

Test the program and check that everything works as intended.

8.6 PHASER

A Phaser looks like a CyclicBarrier, but is more flexible. In the same way as with a CyclicBarrier you can be achieve that a number of threads are waiting at the barrier, until the last thread is arriving, but in contrast to a CyclicBarrier, that waits for a fixed number of threads, a Phaser act as a barrier for a variable number threads, and there may be several phases – barriers. The following program starts 5 threads in addition to the primary thread, and the execution of the 6 threads are synchronized by a Phaser object with 4 phases:

package thread20;

import java.util.*;

import java.util.concurrent.*;

public class Thread20 {

public static void main(String[] args) {

Phaser phaser = new Phaser(1);

Thread thread1 = new Thread(new Worker(phaser), "Thread-1");

Thread thread2 = new Thread(new Worker(phaser), "Thread-2");

Thread thread3 = new Thread(new Worker(phaser), "Thread-3");

Thread thread4 = new Thread(new Worker(phaser), "Thread-4");

Thread thread5 = new Thread(new Worker(phaser), "Thread-5");

System.out.println("\n--- Start Phaser ---");

thread1.start();

Test the program and check that everything works as intended.

8.6 PHASER

A Phaser looks like a CyclicBarrier, but is more flexible. In the same way as with a CyclicBarrier you can be achieve that a number of threads are waiting at the barrier, until the last thread is arriving, but in contrast to a CyclicBarrier, that waits for a fixed number of threads, a Phaser act as a barrier for a variable number threads, and there may be several phases – barriers. The following program starts 5 threads in addition to the primary thread, and the execution of the 6 threads are synchronized by a Phaser object with 4 phases:

JAVA 8:MULTITHREADED PROGRAMS

74

COnCURREnCy TOOLS

public static void main(String[] args) {

// creates a thread to a Writer object which adds boy names // creates a thread to a Writer object which adds girl names // Creates 10 Reader threads, when it should be daemon threads // start all the threads

// the primary thread should join the two writer threds }

}

Test the program and check that everything works as intended.

8.6 PHASER

A Phaser looks like a CyclicBarrier, but is more flexible. In the same way as with a CyclicBarrier you can be achieve that a number of threads are waiting at the barrier, until the last thread is arriving, but in contrast to a CyclicBarrier, that waits for a fixed number of threads, a Phaser act as a barrier for a variable number threads, and there may be several phases – barriers. The following program starts 5 threads in addition to the primary thread, and the execution of the 6 threads are synchronized by a Phaser object with 4 phases:

package thread20;

import java.util.*;

import java.util.concurrent.*;

public class Thread20 {

public static void main(String[] args) {

Phaser phaser = new Phaser(1);

Thread thread1 = new Thread(new Worker(phaser), "Thread-1");

Thread thread2 = new Thread(new Worker(phaser), "Thread-2");

Thread thread3 = new Thread(new Worker(phaser), "Thread-3");

Thread thread4 = new Thread(new Worker(phaser), "Thread-4");

Thread thread5 = new Thread(new Worker(phaser), "Thread-5");

System.out.println("\n--- Start Phaser ---");

thread1.start();

JAVA 8:MULTITHREADED PROGRAMS ConCurrenCy tools

JAVA 8:MULTITHREADED PROGRAMS COnCURREnCy TOOLS

phaser.arriveAndDeregister();

if(phaser.isTerminated()) System.out.println("\nThe Phaser object terminated");

}

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

In document Java 8 Multithreaded Programs (Page 72-75)