单项选择题

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


您可能感兴趣的试卷

你可能感兴趣的试题

2.单项选择题

public class TestOne { 
public static void main (String[] args) throws Exception { 
Thread.sleep(3000); 
System.out.println(”sleep”); 
} 
} 
What is the result?() 

A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints “sleep”.
D. The code executes normally, but nothing is printed.

3.多项选择题

public class Threads2 implements Runnable { 
public void nun() { 
System.out.println(”run.”); 
throw new RuntimeException(”Problem”); 
} 
public static void main(String[] args) { 
Thread t = new Thread(new Threads2()); 
t.start(); 
System.out.println(”End of method.”); 
} 
} 
Which two can be results?()

A. java.lang.RuntimeException: Problem
B. run.    java.lang.RuntimeException: Problem
C. End of method.    java.lang.RuntimeException: Problem
D. End of method.      run.      java.lang.RuntimeException: Problem
E. run.    java.lang.RuntimeException: Problem     End of method.

6.单项选择题

public class Threads5 { 
public static void main (String[] args) { 
new Thread(new Runnable() { 
public void run() { 
System.out.print(”bar”); 
}
}).start(); 
} 
} 
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.

8.单项选择题

public class Threads3 implements Runnable { 
public void run() { 
System.out.print(”running”); 
} 
public static void main(String[] args) { 
Thread t = new Thread(new Threads3()); 
t.run(); 
t.run(); 
t.start(); 
} 
} 
What is the result?() 

A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes and prints “running”.
D. The code executes and prints “runningrunning”.
E. The code executes and prints “runningrunningrunning”.

9.多项选择题Which two code fragments will execute the method doStuff() in a separate thread?()

A. new Thread() { public void run() { doStuff(); } }
B. new Thread() { public void start() { doStuff(); } }
C. new Thread() { public void start() { doStuff(); } } .run();
D. new Thread() { public void run() { doStuff(); } } .start();
E. new Thread(new Runnable() { public void run() { doStuff(); } } ).run();
F. new Thread(new Runnable() { public void run() { doStuff(); } }).start();

10.单项选择题

12. String csv = “Sue,5,true,3”; 
13. Scanner scanner = new Scanner( csv); 
14. scanner.useDelimiter(”,”); 
15. int age = scanner.nextInt(); 
What is the result?() 

A. Compilation fails.
B. After line 15, the value of age is 5.
C. After line 15, the value of age is 3.
D. An exception is thrown at runtime.