Menu
support@authoritypapers.com
+1(805) 568 7317

a barrier is a thread synchronization mechanism that allows several threads to run f 5187466

A barrier is a thread synchronization mechanism that allows several threads to run for a period and then forces all threads to wait until all have reached a certain point. Once all threads have reached this point (the barrier), they may all continue. An interface for a barrier appears as follows:

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();

}

The following code segment establishes a barrier and creates 10 worker threads that will synchronize according to the barrier:

Barrier barrier = new BarrierImpl(THREAD_COUNT);

Thread[] workers = new Thread[THREAD_COUNT];

for (int i = 0; i

{

workers[i] = new Thread(new Worker(barrier));

workers[i].start();

}

Note that the barrier must be initialized to the number of threads that are being synchronized and that each thread has a reference to the same barrier object (jersey). Each worker will run as follows:

// All threads have access to this barrier Barrier jersey;

// do some work for a while …

// now wait for the others Jersey.waitForOthers();

// now do more work …

When a thread invokes the method waitForOthers( ), it will block until all threads have reached this method (the barrier). Once all threads have reached the method, they may all proceed with the remainder of their code. The freeAll( ) method bypasses the need to wait for threads to reach the barrier; as soon as freeAll( ) is invoked, all threads waiting for the barrier are released.

((Implement the barrier interface code using Java synchronization))

“show example of the answer”

"Order a similar paper and get 15% discount on your first order with us
Use the following coupon
"GET15"

Order Now