Question: Given the following program, how many times is Locked! expected to be printed? Assume 100 milliseconds is enough time for each task created by the

Given the following program, how many times is Locked! expected to be printed? Assume 100 milliseconds is enough time for each task created by the program to complete. 

import java.util.concurrent.locks.*; public class Padlock { private Lock lock = new ReentrantLock();

A. One time.

B. Five 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.locks.*; public class Padlock { private Lock lock = new ReentrantLock(); public void lockUp() { if (lock.tryLock()) { lock. lock(); } } } System.out.println("Locked!"); } public static void main(String... unused) throws Exception { var gate= new Padlock(); for (int i=0; i gate. lockUp()) .start(); Thread.sleep(100); } lock.unlock();

Step by Step Solution

3.37 Rating (153 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The provided code is a Java program that demonstrates the usage of locks for concurrent execution Th... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Oracle Questions!