1Z0-809 practice question 3 of 25
Given: class Worker extends Thread { CyclicBarrier cb; public Worker(CyclicBarrier cb) { this.cb = cb; } public void run () { try { cb.await()…
Full set: 196 questionsThe goal is to execute the run methods of both Worker and Master. A CyclicBarrier can be configured with a barrierAction (a Runnable) that executes when the barrier is tripped. By setting the number of parties to 1 (new CyclicBarrier(1, master)), the barrier will trip as soon as the single Worker thread calls cb.await(). This immediately triggers the master Runnable to execute its run method. After the barrier action completes, the Worker thread is unblocked and proceeds to execute the rest of its run method, printing "Worker...".
A. A CyclicBarrier with 2 parties will cause the Worker thread to wait indefinitely because only one thread (worker) calls await(), and the barrier is never tripped.
B. Changing Master to extend Thread does not cause its run method to be executed, as no Master thread is ever started with .start().
D. The CyclicBarrier class does not have a constructor that accepts only a Runnable argument. This line would result in a compilation error.
1. Oracle, "Java SE 8 API Specification, java.util.concurrent.CyclicBarrier," docs.oracle.com. The constructor CyclicBarrier(int parties, Runnable barrierAction) is documented, which creates a new CyclicBarrier that will trip when the given number of parties (parties) are waiting upon it, and which will execute the given barrier action when the barrier is tripped.
2. Goetz, B., et al. (2006). Java Concurrency in Practice. Addison-Wesley Professional. Chapter 5.4, "Barriers," explains the mechanics of CyclicBarrier, including the barrier action.
Keep practicing
Take the free 1Z0-809 practice test
Ten exam-style questions with answers and explanations, plus the exam facts and study guides.
More questions
Other 1Z0-809 practice questions
- Question 2Given: class Student { String course, name, city; public Student (String name, String cou…
- Question 4Given the code fragment: Path file = Paths.get (“courses.txt”); // line n1 Assume the cou…
- Question 5Given that /green.txt and /colors/yellow.txt are accessible, and the code fragment: Path …
- Question 6Given the code fragment:List<String> empDetails = Arrays.asList(“100, Robin, HR”,“200, Ma…
- Question 7Given the code fragment: List str = Arrays.asList (“my”, “pen”, “is”, “your’, “pen”); Pre…
- Question 8Which two reasons should you use interfaces instead of abstract classes? (Choose two.)
