Which statements are true about the following program?
public class Main { private static Thread t1 = new Thread("T1") { public void run() { try {/*w w w. ja v a 2 s. co m*/ wait(1000); } catch (InterruptedException ie) { } } }; private static Thread t2 = new Thread("T2") { public void run() { notify(); } }; private static Thread t3 = new Thread("T3") { public void run() { yield(); } }; private static Thread t4 = new Thread("T4") { public void run() { try { sleep(100); } catch (InterruptedException ie) { } } }; public static void main(String[] args) { t1.start(); t2.start(); t3.start(); t4.start(); try { t4.join(); } catch (InterruptedException ie) { } } }
Select the three correct answers.
sleep()
method in a try-catch construct in thread t4 is unnecessary.join()
method in a try-catch construct in the main thread is necessary.(b), (c), and (f)
The wait()
and notify()
methods of the Object class can only be called on an object whose lock the thread holds, otherwise a java.lang.IllegalMonitorStateException is thrown.
The static method yield()
of the class Thread does not throw any exceptions.
Both the sleep()
and join()
methods of the Thread class throw an InterruptedException.