Phaser Java Example

By Arvind Rai, January 02, 2014
java.util.concurrent.Phaser has been introduced in JDK 7. A Phaser is a synchronization barrier that can be reused with the time. Phaser is different from other barriers like CyclicBarrier . The basic task of Phaser is that it registers the task, start and then deregisters. There are different methods in Phaser. We will study some of them. First we will see how to initialize Phaser in java.
Phaser phaser = new Phaser()
Phaser phaser = new Phaser(int  task)
 
A Phaser can be initialized directly or by passing number of task. When we initialize without passing number of task, it means there is no registered parties initially. And if number is given then a Phaser is initialized with given number of registered unarrived parties. Now find some method description

register() in Java Phaser

It registers the unarrived parties to Phaser. If the parent is registered, its child will also be registered. This method waits for completion, if onAdvance() method is running.

onAdvance() in Java Phaser

onAdvance methods returns boolean value. When it returns true, Phaser will reach to termination point. This method is overridden to return conditional Boolean values. By default it value is true.

arriveAndAwaitAdvance() in Java Phaser

Party arrives to Phaser and awaits to complete others. The use of arriveAndAwaitAdvance() method is to await in the case of interruption or timeout.

arriveAndDeregister() in Java Phaser

Party arrives to Phaser and de registers to itself. If deregistration makes a phaser of zero party and that Phaser has parent also then Phaser will also be deregistered from parent.

isTerminated() in Java Phaser

It returns Boolean value. If terminated the returns true otherwise false.

Now look at the example.
PhaserDemo.java
package com.concretepage.util.concurrent;
import java.util.List;
import java.util.concurrent.Phaser;
public class PhaserDemo {
	public void doTasks(List<Runnable> tasks) {
	   final Phaser phaser = new Phaser(); 
	   for (final Runnable task : tasks) {
	     phaser.register();
	     new Thread() {
	       public void run() {
	         phaser.arriveAndAwaitAdvance(); 
	         task.run();
	       }
	     }.start();
	   }
	   phaser.arriveAndDeregister();
	}
	
	public void initializeTasks(List<Runnable> tasks, final int iterations) {
	   final Phaser phaser = new Phaser() {
	     protected boolean onAdvance(int phase, int registeredParties) {
	       return phase >= iterations || registeredParties == 0;
	     }
	   };
	   phaser.register();
	   for (final Runnable task : tasks) {
	     phaser.register();
	     new Thread() {
	       public void run() {
	         do {
	           task.run();
	           phaser.arriveAndAwaitAdvance();
	         } while (!phaser.isTerminated());
	       }
	     }.start();
	   }
	   phaser.arriveAndDeregister(); 
	}
}
 
There is two method doTasks and initializeTasks and both are the sample of using Phaser. Now find runnbale task and the main class to two run the demo.
MainClass.java
package com.concretepage.util.concurrent;
import java.util.ArrayList;
import java.util.List;
public class MainClass {
	public static void main(String[] args) {
		PhaserDemo phaserDemo = new PhaserDemo();
		RunnableTask task1 = new RunnableTask("Task one");
		RunnableTask task2 = new RunnableTask("Task two");
		List<Runnable> tasks = new ArrayList<Runnable>();
		tasks.add(task1);
		tasks.add(task2);
		phaserDemo.doTasks(tasks);
		phaserDemo.initializeTasks(tasks,2);
	}
}
 
RunnableTask.java
package com.concretepage.util.concurrent;
public class RunnableTask implements Runnable{
	String msg = null;
	public RunnableTask(String msg){
		this.msg = msg;
	}
	@Override
	public void run() {
		try {
			Thread.sleep(1000);
			System.out.println(msg +" done.");
		} catch (InterruptedException e) {
			System.out.println(e);
		}
	}
}
 
POSTED BY
ARVIND RAI
ARVIND RAI







©2024 concretepage.com | Privacy Policy | Contact Us