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

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

run methods of both the Worker and Master classes are executed.

You have been asked to ensure that the

Which modification meets the requirement?

line n2, insert CyclicBarrier cb = new CyclicBarrier(2, master);

    Correct Answer: A

    In order to ensure that the run methods of both the Worker and Master classes are executed, the Master class should also be a thread, which allows it to be run concurrently with the Worker thread. By modifying the Master class to extend Thread at line n1, we allow it to be started and executed as a separate thread, thus ensuring that both Worker and Master can run their run methods. Without this change, the CyclicBarrier alone cannot ensure that the run method of Master is executed.

Discussion
thetechOption: B

Answer is B. Tested.

InnovationOption: B

B is correct

asdfjhfgjuaDCVOption: B

B is the Correct answer tested.

steefaandOption: B

B is correct.

iSnoverOption: B

Answer is B. Tested.

WilsonKKerllOption: B

Answer is B. Tested.