單項選擇題

class Computation extends Thread { 
private int num; 
private boolean isComplete; 
private int result; 
public Computation(int num) { this.num = num; } 
public synchronized void run() { 
result = num * 2; 
isComplete = true; 
notify(); 
} 
public synchronized int getResult() { 
while (!isComplete) { 
try { 
wait(); 
} catch (InterruptedException e) { } 
} 
return result; 
} 
public static void main(String[] args) { 
Computation[] computations = new Computation [4]; 
for (int i = 0; i < computations.length; i++) { 
computations[i] = new Computation(i); 
computations[i] .start(); 
} 
for (Computation c : computations) 
System.out.print(c.getResult() +“ “); 
} 
} 
What is the result?() 

A. The code will deadlock.
B. The code may run with no output.
C. An exception is thrown at runtime.
D. The code may run with output “0 6”.
E. The code may run with output “2 0 6 4‟.
F. The code may ruin with output “0 2 4 6”.

題目列表

你可能感興趣的試題

多項選擇題

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.

微信掃碼免費搜題