Exam 1z0-809 All QuestionsBrowse all questions from this exam
Question 57

Given:

class Worker extends Thread {

CyclicBarrier cb;

public Worker(CyclicBarrier cb) { this.cb = cb; }

public void run () {

try {

cb.await();

System.out.println("Worker"¦");

} catch (Exception ex) { }

}

}

class Master implements Runnable { //line n1

public void run () {

System.out.println("Master"¦");

}

}

and the code fragment:

Master master = new Master();

//line n2

Worker worker = new Worker(cb);

worker.start();

You have been asked to ensure that the run methods of both the Worker and Master classes are executed.

Which modification meets the requirement?

    Correct Answer: A

    To ensure that the run methods of both Worker and Master classes are executed, you need to create a CyclicBarrier that waits for two parties (threads) and executes the Master runnable when the barrier condition is met. Therefore, the correct modification is to insert CyclicBarrier cb = new CyclicBarrier(2, master) at line n2. This ensures that two threads (Worker and Master) reach the barrier and then the Master runnable is executed, followed by the Worker thread proceeding after the await call.

Discussion
aymanjOption: C

Correct answer is C .. CyclicBarrier constructor takes as arguments the maximum number of threads until release and a Runnable Object .. CyclicBarrier cb = new CyclicBarrier( int i , RunnableObject) ;

Svetleto13Option: C

C,tested

asdfjhfgjuaDCVOption: C

C is the Correct answer

steefaandOption: C

C is correct, since second argument for CyclicBarrier is Runnable which will be executed when barrier is fulfilled.

WilsonKKerllOption: C

Answer is C. Output : Master! Worker. Question is 'both the Worker and Master classes are executed'. If new CyclicBarrier(1); is output : Worker.

lxktnjjnOption: B

Answer is B. Constructors for CyclicBarrier CyclicBarrier(int parties), CyclicBarrier(int parties, Runnable barrierAction) - Creates a new CyclicBarrier that will trip when the given number of parties (threads) are waiting upon it, and which will execute the given barrier action when the barrier is tripped, performed by the last thread entering the barrier So 2nd parameter is what will be done after tripping barrier.

lxktnjjn

In question there is no need to execute Master 2nd time.

xijxeyho

C is OK. Master is executed as Runnable.