Which code, when inserted at (1), will result in the program compiling and printing Done on the standard input stream, and then all threads terminating normally?
public class Main { private static Thread t1 = new Thread("T1") { public void run() { synchronized(Main.class) { try {/*w w w. j av a2 s . c o m*/ // (1) INSERT CODE HERE ... } catch (InterruptedException ie){ ie.printStackTrace(); } System.out.println("Done"); }}}; public static void main(String[] args) { t1.start(); try { t1.join(); } catch (InterruptedException ie){ ie.printStackTrace(); } } }
Select the two correct answers.
wait()
;wait()
;yield()
;(d) and (f)
(a) and (b) result in a java.lang.IllegalMonitorStateException, as the t1 thread does not hold a lock on the current object, i.e., on the thread itself.
(c) The t1 thread will wait forever, as it never gets any notification, and the main thread also waits forever for t1 to complete.
(e) The yield()
method does not throw the checked InterruptedException, and the compiler reports an error as the code in the catch block is unreachable.