Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In this question you are to write a Barrier class with a constructor and one oth

ID: 3849738 • Letter: I

Question

In this question you are to write a Barrier class with a constructor and one other method, checkpoint: public class Barrier { public Barrier(int n) {...} public boolean checkpoint() {...} } After a Barrier object has been created, every time a thread invokes checkpoint () on that Barrier object it will pause until n threads have invoked checkpoint () at which time all n threads will continue. The checkpoint () method returns true if the thread successfully synchronized, and false otherwise (e.g. this thread was the n+1 the thread). a) Write the Barrier class. Ensure each Barrier object is left in a state so that it can be reused. b) Draw a class diagram for the Barrier class.

Explanation / Answer

Public class Barrier implements Runnable {

String message["hai', "hello"] ;

public Barrier(int n) {

this. n=n ;

}

Public boolean checkPoint()

{

while(true)

{

If(isWaiting(message)

Synchronized(message[0])

{message[0].notify();

}

Else

Return false;

}

}

Void run()

{for(int i=0;i<message.length;i++)

wait();

System. Out. Println(n+message[i]) ;

}

}

Wait()

try{

Thread. currentThread. Sleep(100*math.random())}

Catch(interruptedExpection x) {

System. Oit. Println("interruption") ;

}

}

Class Thread

{

Public static void main(String main[])

Thread t1=new Thread(new Barrier("t1:") ;

Thread t2=new Thread(new Barrier("t2:") ;

t1. start() ;

t2. start() ;

boolean thread1IsAlive=true;

boolean thread2IsAlive=true;

do{

if(thread1IsAlive &&! thread1. IsAlive()

{

thread1IsAlive =false;

System. Out. Println("thread 1 is dead") ;

} if(thread2IsAlive & &! thread2. isAlive())

{

thread2IsAlive=false;

System. Out. Println("thread2 d3ad") ;

} while(thread1IsAlive || thtead2Alive)

}

This program is a multithreading ptogram where we can run thteads synchronously and wait() methods lets the thread to wait for some time while other thread is executing and run and start methods ro run tge program

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote