Review Questions
13.15 |
Which one of these events will cause a thread to die? Select the one correct answer.
|
13.16 |
Which statements are true about the following code? public class Joining { static Thread createThread(final int i, final Thread t1) { Thread t2 = new Thread() { public void run() { System.out.println(i+1); try { t1.join(); } catch (InterruptedException ie) { } System.out.println(i+2); } }; System.out.println(i+3); t2.start(); System.out.println(i+4); return t2; } public static void main(String[] args) { createThread(10, createThread(20, Thread.currentThread())); } } Select the two correct answers.
|
13.17 |
Which statements are true about the following program? public class ThreadAPI { private static Thread t1 = new Thread("T1") { public void run() { try { 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.
|
13.18 |
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 RunningThreads { private static Thread t1 = new Thread("T1") { public void run() { synchronized(RunningThreads.class) { try { // (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.
|
13.19 |
What can be guaranteed by calling the method yield()? Select the one correct answer.
|
13.20 |
In which class or interface is the notify() method defined? Select the one correct answer.
|
13.21 |
How can the priority of a thread be set? Select the one correct answer.
|
13.22 |
Which statements are true about locks? Select the two correct answers.
|
13.23 |
What will be the result of invoking the wait() method on an object without ensuring that the current thread holds the lock of the object? Select the one correct answer.
|
13.24 |
Which of these are plausible reasons why a thread might be alive, but still not be running? Select the four correct answers.
|
13.25 |
What will the following program print when compiled and run? public class Tank { private boolean isEmpty = true; public synchronized void emptying() { pause(true); isEmpty = !isEmpty; System.out.println("emptying"); notify(); } public synchronized void filling() { pause(false); isEmpty = !isEmpty; System.out.println("filling"); notify(); } private void pause(boolean flag) { while(flag ? isEmpty : !isEmpty) { try { wait(); } catch (InterruptedException ie) { System.out.println(Thread.currentThread() + " interrupted."); } } } public static void main(String[] args) { final Tank token = new Tank(); (new Thread("A") { public void run() {for(;;) token.emptying();}}).start(); (new Thread("B") { public void run() {for(;;) token.filling();}}).start(); } } Select the one correct answer.
|
13.26 |
What will the following program print when compiled and run? public class Syncher2 { final static int[] intArray = new int[2]; private static void pause() { while(intArray[0] == 0) { try { intArray.wait(); } catch (InterruptedException ie) { System.out.println(Thread.currentThread() + " interrupted."); } } } public static void main (String[] args) { Thread runner = new Thread() { public void run() { synchronized (intArray) { pause(); System.out.println(intArray[0] + intArray[1]); }}}; runner.start(); intArray[0] = intArray[1] = 10; synchronized(intArray) { intArray.notify(); } } } Select the one correct answer.
|