Question: Given the following program, how many times is TV Time expected to be printed? Assume 10 seconds is enough time for each task created by
Given the following program, how many times is TV Time expected to be printed? Assume 10 seconds is enough time for each task created by the program to complete.

A. One time.
B. Three times.
C. The code does not compile.
D. The code hangs indefinitely at runtime.
E. The code throws an exception at runtime.
F. The output cannot be determined ahead of time.
import java.util.concurrent.*; import java.util.concurrent.locks. *; public class Television { private static Lock myTurn = new ReentrantLock(); public void watch() { try { if (myTurn. lock (5, TimeUnit. SECONDS)) { System.out.println("TV Time"); myTurn.unlock(); } } catch (InterruptedException e) {} } public static void main(String[] t) throws Exception { var newTv = new Television(); for (int i = 0; i < 3; i++) { new Thread (() -> newTv.watch()).start(); Thread.sleep(10*1000); } }}
Step by Step Solution
3.43 Rating (153 Votes )
There are 3 Steps involved in it
The code provided seems to be a Java program that uses concurrency specifically locks from the javau... View full answer
Get step-by-step solutions from verified subject matter experts
