Which of the following are possible results of running the following application? (Choose all that apply.)
import java.util.concurrent.*; import java.util.concurrent.locks.*; ? public class Main { public static void main(String[] args) throws Exception { ? ExecutorService service = null; try { //w w w .j a va 2 s . com ReadWriteLock lock = new ReentrantReadWriteLock(); // m1 service = Executors.newFixedThreadPool(20); service.submit(() -> { lock.writeLock().lock(); // m2 System.out.println("Got Write Lock!"); }); for (int i = 0; i < 10; i++) { service.submit(() -> { lock.readLock().lock(); // m3 System.out.println("Got Read Lock!"); }); } } finally { if (service != null) service.shutdown(); } System.out.print("Finished"); } }
A,B,C,D.
The application compiles without issue so E, F, and G are incorrect.
The problem with this code is that none of the locks that are acquired are ever released.
Since the executor service is pooled, the requests could come in any order, so A and B are both possible outputs and therefore are correct answers.
C is also possible, because Finished could be outputted before any of the threads have requested their first lock.
Since the tasks could be processed in any order and none of the locks are released, the write lock request will hang indefinitely if any read lock requests have been granted and vice versa, making D a correct answer.
H is incorrect, since there is nothing in the code that will produce a runtime exception.