多项选择题

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.

相关考题

public class TwoThreads {  private static Object resour...


单项选择题

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