| 9.5 | Given the following program, which statements are guaranteed to be true? public class ThreadedPrint { static Thread makeThread(final String id, boolean daemon) { Thread t = new Thread(id) { public void run() { System.out.println(id); } }; t.setDaemon(daemon); t.start(); return t; } public static void main(String[] args) { Thread a = makeThread("A", false); Thread b = makeThread("B", true); System.out.print("End\n"); } } Select the two correct answers. -
The letter A is always printed. -
The letter B is always printed. -
The letter A is never printed after End . -
The letter B is never printed after End . -
The program might print B , End and A, in that order. | | 9.6 | Which statement is true? Select the one correct answer. -
No two threads can concurrently execute synchronized methods on the same object. -
Methods declared synchronized should not be recursive, since the object lock will not allow new invocations of the method. -
Synchronized methods can only call other synchronized methods directly. -
Inside a synchronized method, one can assume that no other threads are currently executing any other methods in the same class. | | 9.7 | Given the following program, which statement is true? public class MyClass extends Thread { static Object lock1 = new Object(); static Object lock2 = new Object(); static volatile int i1, i2, j1, j2, k1, k2; public void run() { while (true) { doit(); check(); } } void doit() { synchronized(lock1) { i1++; } j1++; synchronized(lock2) { k1++; k2++; } j2++; synchronized(lock1) { i2++; } } void check() { if (i1 != i2) System.out.println("i"); if (j1 != j2) System.out.println("j"); if (k1 != k2) System.out.println("k"); } public static void main(String[] args) { new MyClass().start(); new MyClass().start(); } } Select the one correct answer. -
The program will fail to compile. -
One cannot be certain whether any of the letters i , j , and k will be printed during execution. -
One can be certain that none of the letters i , j , and k will ever be printed during execution. -
One can be certain that the letters i and k will never be printed during execution. -
One can be certain that the letter k will never be printed during execution. | |