Java OCA OCP Practice Question 2493

Question

Given:

2. public class Main implements Runnable {    
3.   public static void main(String[] args) {  
4.     Main d = new Main();  
5.     new Thread(d).start();  
6.     Thread t1 = new Thread(d);  
7.     t1.start();  /*from   w w  w.ja v a 2s. c  om*/
8.   }  
9.   public void run() {  
10.     for(int j = 0; j < 4; j++) {  do1();   do2();  }  
11.   }  
12.   void do1() {  
13.     try { Thread.sleep(1000); }  
14.     catch (Exception e) { System.out.print("e "); }  
15.   }  
16.   synchronized void do2() {  
17.     try { Thread.sleep(1000); }  
18.     catch (Exception e) { System.out.print("e "); }  
19. } } 

Which are true? (Choose all that apply.)

  • A. Compilation fails.
  • B. The program's minimum execution time is about 8 seconds.
  • C. The program's minimum execution time is about 9 seconds.
  • D. The program's minimum execution time is about 12 seconds.
  • E. The program's minimum execution time is about 16 seconds.
  • F. Un-synchronizing do2() changes the program's minimum execution time by only a few milliseconds.


C is correct.

Note

Both thread's first invocation of do1() will start at about the same time.

After that second has elapsed, one of the threads will invoke do2(), and the other will have to wait.

Once the thread that ran do2() first is done with do2(), the other thread can run do2().

Thereafter, the threads will alternate running do1() and do2(), but they are able to run mostly simultaneously.

F is incorrect because un-synchronizing do2() will shave about 1000 milliseconds off the elapsed time.




PreviousNext

Related