This is from operating system consept with java 8th edition ch.6 Q.41 A barrier
ID: 3661643 • Letter: T
Question
This is from operating system consept with java 8th edition ch.6 Q.41
A barrier is a thread synchronization mechanism .that allows several threads to run for a period and risen forces all threads to s-ar. until all have a 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: The following code segment establishes a barrier and creates 10 worker threads hat will synchronize according to the barrier Note that the barrier must be initialized to the number of threads that are being synchronized and that each thread that a reference to the same barrier object (jersey) Each worker will run as follows When a thread invokes .he method wait For Others( ), it will block until all threads have reached this method (the barrier). Once all threads have readied the method, they may all proceed wills the remainder of their code The free AII ( ) method bypasses the need to wait for threads to reach the barrier, as soon as free AII ( ) is invoked, all threads a waiting for tine barrier are released. Implement the harrier interface code using java synchronization.Explanation / Answer
First of all i would like to say that this question is modified to check the actuall question see
operating system consept with java 8th edition ch.6 Q.41 the actual question is following
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 thelast 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:
public static final int THREAD COUNT = 10;
Barrier jersey = new BarrierImpl(THREAD COUNT);
for (int i = 0; i < THREAD COUNT; i++)
(new Worker(jersey)).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 using Java synchronization.
Solution of Your question is following:
public interface Barrier
{
public void waitForOthers();
public void freeAll();
}
public static final int THREAD COUNT = 10;
public void waitForOthers()
{
// write the code for Wait the thread
//you will use lock() and sleep() method to do this. here i use the sleep method
sleep((long)(3000*Math.random()));
}
public void freeAll()
{
for (int i = 0; i < THREAD COUNT; i++)
{
System.out.println(i);//This line is used for print the output
}
}
Barrier barrier= new BarrierImpl(THREAD COUNT);
Thread[] workers=new Thread(THREAD COUNT);
for (int i = 0; i < THREAD COUNT; i++)
{
workers[i]=new Thread(new Worker(barrier));
workers[i].start();
}
when the thread start the new Worker class object initialize to call the 'barrier' object and gives the folloeing output
0 1 2 3 4 5 6 7 8 9
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.