Java SE 8 Programmer II: Ensure Execution of Worker and Master Classes' Run Methods

Ensure Execution of Worker and Master Classes' Run Methods

Question

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?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

C.

The code fragment creates an instance of the Master class and an instance of the Worker class. The Worker class extends the Thread class and uses a CyclicBarrier object to synchronize its execution with the execution of the Master class.

A CyclicBarrier is a synchronization aid that allows a set of threads to wait for each other to reach a common barrier point. When the required number of threads have arrived, the barrier is released, and all threads can proceed.

To ensure that the run() methods of both classes are executed, we need to ensure that both the Worker and Master threads reach the barrier before any of them continue.

The CyclicBarrier constructor takes two arguments: the number of parties (threads) that must arrive at the barrier before it is released, and an optional action that is executed once the barrier is released. The action is specified as a Runnable object.

Option A: CyclicBarrier cb = new CyclicBarrier(2, master);

This option creates a CyclicBarrier with a party size of 2 and an action that is set to the Master instance. This means that two parties (threads) are required to reach the barrier before it is released, and once the barrier is released, the run() method of the Master class will be executed.

This option ensures that both the Worker and Master threads reach the barrier before any of them continue, so it meets the requirement.

Option B: CyclicBarrier cb = new CyclicBarrier(1);

This option creates a CyclicBarrier with a party size of 1. This means that only one party (thread) is required to reach the barrier before it is released.

Since we have two parties (the Worker and Master threads), this option does not ensure that both threads reach the barrier before any of them continue, so it does not meet the requirement.

Option C: CyclicBarrier cb = new CyclicBarrier(1, master);

This option creates a CyclicBarrier with a party size of 1 and an action that is set to the Master instance.

Like Option B, this option does not ensure that both the Worker and Master threads reach the barrier before any of them continue, so it does not meet the requirement.

Option D: CyclicBarrier cb = new CyclicBarrier(master);

This option is incorrect because the CyclicBarrier constructor does not take a single argument of type Runnable. The master object needs to be passed as the action to the CyclicBarrier constructor, along with the party size.

Therefore, Option A is the correct answer, as it ensures that both the Worker and Master threads reach the barrier before any of them continue.