Page: 5
9. Multi-Threading
Q: 17 Given:
foo and bar are public references available to many other threads. foo refers to a Thread and bar is an
Object. The thread foo is currently executing bar.wait().
From another thread, what provides the most reliable way to ensure that foo will stop executing wait()?
A. foo.notify(); B. bar.notify();
C. foo.notifyAll(); D. Thread.notify();
E. bar.notifyAll(); F. Object.notify();
Answer: E
Q: 18 Given:
1. public class MyLogger {
2. private StringBuilder logger = new StringBuuilder();
3. public void log(String message, String user) {
4. logger.append(message);
5. logger.append(user);
6. }
7. }
The programmer must guarantee that a single MyLogger object works properly for a multi-threaded system.
How must this code be changed to be thread-safe?
A. synchronize the log method
B. replace StringBuilder with StringBuffer
C. replace StringBuilder with just a String object and use the string concatenation (+=) within the log method
D. No change is necessary, the current MyLogger code is already thread-safe.
Answer: A
Q: 19 Given:
1. public class TestSeven extends Thread {
2. private static int x;
3. public synchronized void doThings() {
4. int current = x;
5. current++;
6. x = current;
7. }
8. public void run() {
9. doThings();
10. }
11.}
Which statement is true?
A. Compilation fails.
B. An exception is thrown at runtime.
C. Synchronizing the run() method would make the class thread-safe.
D. The data in variable "x" are protected from concurrent access problems.
E. Declaring the doThings() method as static would make the class thread-safe.
F. Wrapping the statements within doThings() in a synchronized(new Object()) { } block would make the class thread-safe.
Answer: E
Q:20 Click the Exhibit button.
Which two statements are true if this class is compiled and run? (Choose two.)
A. An exception may be thrown at runtime.
B. The code may run with no output, without exiting.
C. The code may run with no output, exiting normally.
D. The code may run with output "A B A B C C ", then exit.
E. The code may run with output "A B C A B C A B C ", then exit.
F. The code may run with output "A A A B C A B C C ", then exit.
G. The code may run with output "A B C A A B C A B C ", then exit.
Answer: E, G
Q: 21 Given:
1. public class Threads5 {
2. public static void main (String[] args) {
3. new Thread(new Runnable() {
4. public void run() {
5. System.out.print("bar");
6. }}).start();
7. }
8. }
What is the result?
A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "bar".
D. The code executes normally, but nothing prints.
Answer: C
Q: 22 Which three will compile and run without exception? (Choose three.)
A. private synchronized Object o;
B. void go() {
synchronized() { /* code here */ }
C. public synchronized void go() { /* code here */ }
D. private synchronized(this) void go() { /* code here */ }
E. void go() {
synchronized(Object.class) { /* code here */ }
F. void go() {
Object o = new Object();
synchronized(o) { /* code here */ }
Answer: C, E, F
Page: 5
1
2
3
4
5
6
7
8
9
10