单项选择题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, which statement is the most reliable way to ensue that foo will stop executing wait()? 

A. foo.notify();
B. bar.notify();
C. foo.notifyAll();
D. Thread.notify();
E. bar.notiFYAll();
F. Object.notify();


您可能感兴趣的试卷

你可能感兴趣的试题

1.单项选择题

void waitForSignal() { 
Object obj = new Object(); 
synchronized (Thread.currentThread()) { 
obj.wait(); 
obj.notify(); 
} 
} 
Which is true?() 

A. This code may throw an InterruptedException.
B. This code may throw an IllegalStateException.
C. This code may throw a TimeoutException after ten minutes.
D. This code will not compile unless “obj.wait()” is replaced with “((Thread) obj).wait()”.
E. Reversing the order of obj.wait() and obj.notify() may cause this method to complete normally.
F. A call to notify() or notifyAll() from another thread may cause this method to complete normally.

3.多项选择题

public class Transfers { 
public static void main(String[] args) throws Exception { 
Record r1 = new Record(); 
Record r2 = new Record(); 
doTransfer(r1, r2, 5); 
doTransfer(r2, r1, 2); 
doTransfer(r1, r2, 1); 
// print the result 
System.out.println(”rl = “ + r1.get() +“, r2=” + r2.get()); 
} 
private static void doTransfer( 
final Record a, final Record b, final int amount) { 
Thread t = new Thread() { 
public void run() { 
new Clerk().transfer(a, b, amount); 
} 
}; 
t.start(); 
} 
} 
class Clerk { 
public synchronized void transfer(Record a, Record b, int amount){ 
synchronized (a) { 
synchronized (b) { 
a.add(-amount); 
b.add(amount); 
} 
} 
} 
} 
class Record { 
int num=10; 
public int get() { return num; } 
public void add(int n) { num = num + n; } 
} 
If Transfers.main() is run, which three are true?()

A. The output may be “r1 = 6, r2 = 14”.
B. The output may be “r1 = 5, r2 = 15”.
C. The output may be “r1 = 8, r2 = 12”.
D. The code may run (and complete) with no output.
E. The code may deadlock (without completing) with no output.
F. M IllegalStateException or InterruptedException may be thrown at runtime.

4.单项选择题

public class TestSeven extends Thread { 
private static int x; 
public synchronized void doThings() { 
int current = x; 
current++; 
x = current; 
} 
public void run() { 
doThings(); 
} 
} 
Which 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.

5.多项选择题

Which three will compile and rim without exception?()

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 */ } }

6.多项选择题

public class TestFive { 
private int x; 
public void foo() { 
int current = x; 
x = current + 1; 
} 
public void go() { 
for(int i=0;i<5;i++) { 
new Thread() { 
public void run() { 
foo(); 
System.out.print(x + “, “); 
} }.start(); 
}}} 
Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ?()

A. Move the line 12 print statement into the foo() method.
B. Change line 7 to public synchronized void go() {.
C. Change the variable declaration on line 3 to private volatile int x;.
D. Wrap the code inside the foo() method with a synchronized( this ) block.
E. Wrap the for loop code inside the go() method with a synchronized block synchronized(this) { // for loop code here }.

7.多项选择题

import java.util.*; 
public class NameList { 
private List names = new ArrayList(); 
public synchronized void add(String name) { names.add(name); } 
public synchronized void printAll() { 
for (int i = 0; i System.out.print(names.get(i) +“ “);
} 
} 
public static void main(String[] args) { 
final NameList sl = new NameList(); 
for(int i=0;i<2;i++) { 
new Thread() { 
public void ruin() { 
sl.add(”A”); 
sl.add(”B”); 
sl.add(”C”); 
sl.printAll(); 
} 
}.start(); 
} 
} 
} 
Which two statements are true if this class is compiled and run?()

 

A. An exception may be thrown at runtime.
B. The code may run with no output, without exiting.
C.The code may rum with output “A B A B C C “, then exit.
D.The code may ruin with output “A A A B C A B C C “, then exit.
E. The code may rum with output “A B C A B C A B C “, then exit.
F.The code may ruin with output “A B C A A B C A B C “, then exit.

8.多项选择题

public class Threads 1 { 
intx=0; 
public class Runner implements Runnable { 
public void run() { 
int current = 0; 
for(int=i=0;i<4;i++){ 
current = x; 
System.out.print(current + “, “); 
x = current + 2; 
} 
} 
} 
public static void main(String[] args) { 
new Threads1().go(); 
} 
public void go() { 
Runnable r1 = new Runner(); 
new Thread(r1).start(); 
new Thread(r1 ).start(); 
} 
} 
Which two are possible results?()

A. 0, 2, 4, 4, 6, 8, 10, 6,
B. 0, 2, 4, 6, 8, 10, 2, 4,
C. 0, 2, 4, 6, 8, 10, 12, 14,
D. 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
E. 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,

10.单项选择题

public class TwoThreads { 
private static Object resource = new Object(); 
private static void delay(long n) { 
try { Thread.sleep(n); } 
catch (Exception e) { System.out.print(”Error “); } 
} 
public static void main(String[] args) { 
System.out.print(”StartMain “); 
new Thread1().start(); 
delay(1000); 
Thread t2 = new Thread2(); 
t2.start(); 
delay(1000); 
t2.interrupt 
delay(1000); 
System.out.print(”EndMain “); 
} 
static class Thread 1 extends Thread { 
public void run() { 
synchronized (resource) { 
System.out.print(”Startl “); 
delay(6000); 
System.out.print(”End1 “); 
} 
} 
} 
static class Thread2 extends Thread { 
public void run() { 
synchronized (resource) { 
System.out.print(”Start2 “); 
delay(2000); 
System.out.print(”End2 “); 
} 
} 
} 
} 
Assume that sleep(n) executes in exactly m milliseconds, and all other code executes in an insignificant amount of time. What is the output if the main() method is run?() 

A. Compilation fails.
B. Deadlock occurs.
C. StartMain Start1 Error EndMain End1
D. StartMain Start1 EndMain End1 Start2 End2
E. StartMain Start1 Error Start2 EndMain End2 End1
F.StartMain Start1 EndMain End1 Start2 Error End2