Construct a barrier that implements the following API -int init- Initializes the barrier to the specified size -int barrier point-Identifies the barrier point Using synchronization tools in java language
A barrier is a tool for synchronizing the activity of a number of threads.
When a thread reaches a barrier point, it cannot proceed until all other
threads have reached this point as well. When the last thread reaches
the barrier point, all threads are released and can resume concurrent
execution.
Assume that the barrier is initialized to N—the number of threads that
must wait at the barrier point:
init(N);
Each thread then performs some work until it reaches the barrier point:
/* do some work for awhile */
barrier point();
/* do some work for awhile */
Using synchronization tools, construct a barrier
that implements the following API:
• int init(int n)—Initializes the barrier to the specified size.
• int barrier point(void)—Identifies the barrier point. All
threads are released from the barrier when the last thread reaches
this point.
The return value of each function is used to identify error conditions.
Each function will return 0 under normal operation and will return
−1 if an error occurs. A testing harness is provided in the source code
download to test your implementation of the barrier.
Answer:
TestBarrier.java
import java.io.*; public class TestBarrier { public static final int THREAD_COUNT = 5; public static void main(String args[]) throws java.io.IOException { System.out.println("Proper output is that we should see is that all threads"); System.out.println("output an 'A' before reaching the barrier and then a 'B'"); System.out.println("after proceeding through the barrier. Therefore, output"); System.out.println("should appear as a series of 'A's followed by an equal count"); System.out.println("of 'B's. (There should not be an intermingling of 'A's and 'B's."); System.out.println("\n\nPress Enter to begin the test:"); (new BufferedReader(new InputStreamReader(System.in))).read(); // initialize the barrier to the number of threads // waiting upon the barrier Barrier barrier = new BarrierImpl(THREAD_COUNT); Thread[] workers = new Thread[THREAD_COUNT]; for (int i = 0; i < workers.length; i++) { workers[i] = new Thread(new Worker(barrier)); workers[i].start(); } try { for (int i = 0; i < workers.length; i++) workers[i].join(); } catch (InterruptedException ie) { } System.out.println("\n\nPress Enter to begin the freeAll() test:"); (new BufferedReader(new InputStreamReader(System.in))).read(); barrier = new BarrierImpl(THREAD_COUNT + 1); workers = new Thread[THREAD_COUNT]; for (int i = 0; i < workers.length; i++) { workers[i] = new Thread(new Worker(barrier)); workers[i].start(); } try { Thread.sleep(3000); } catch (InterruptedException ie) { } barrier.freeAll(); } } /** * A worker using the Barrier */ class Worker implements Runnable { private Barrier partA; public Worker(Barrier partA) { this.partA = partA; } /** * Each thread will do some work for awhile, and then * invoke waitForOthers(). A thread may proceed when all * other threads have arrived at the barrier. */ public void run() { System.out.println("A"); SleepUtilities.nap(); partA.waitForOthers(); System.out.println("B"); } }
SleepUtilities.java
public class SleepUtilities { /** * Nap between zero and NAP_TIME seconds. */ public static void nap() { nap(NAP_TIME); } /** * Nap between zero and duration seconds. */ public static void nap(int duration) { int sleeptime = (int) (NAP_TIME * Math.random() ); try { Thread.sleep(sleeptime*1000); } catch (InterruptedException e) {} } private static final int NAP_TIME = 5; }
BarrierImpl.java
public class BarrierImpl implements Barrier { // the number of threads to wait for private int waitForCount; // Constructor that defines the number of threads in the group for this // Barrier. public BarrierImpl (int waitForCount) { this.waitForCount = waitForCount; } /** * Each thread calls this method when it reaches the barrier. * All threads are released to continue processing when the * last thread calls this method. */ public synchronized void waitForOthers() { --waitForCount; if (waitForCount<= 0) notifyAll(); while (waitForCount> 0) { try { wait(); } catch (InterruptedException ie) { } } } /** * Release all threads from waiting for the barrier. * Any future calls to waitForOthers() will not wait * until the Barrier is set again with a call to the constructor. */ public synchronized void freeAll() { waitForCount= 0; notifyAll(); } }
Barrier.java
public interface Barrier { /** * Each thread calls this method when it reaches the barrier. * All threads are released to continue processing when the * last thread calls this method. */ public void waitForOthers(); /** * Release all threads from waiting for the barrier. * Any future calls to waitForOthers() will not wait * until the Barrier is set again with a call to the constructor. */ public void freeAll(); }
Leave a reply