Java OCA OCP Practice Question 3070

Question

Given:

public class Main implements Runnable {
   void doShape() {
   }//from   w w  w  . j  a  v  a2  s  .  co  m

   synchronized void doSynch() {
      try {
         Thread.sleep(1000);
      } catch (Exception e) {
         System.out.print("e ");
      }
   }

   public static void main(String[] args) {
      long start = System.currentTimeMillis();
      new Thread(new Main()).start();
      Thread t1 = new Thread(new Main());
      t1.start();
      try {
         t1.join();
      } catch (Exception e) {
         System.out.print("e ");
      }
      System.out.println("elapsed: " + (System.currentTimeMillis() - start));
   }

   public void run() {
      for (int j = 0; j < 4; j++) {
         doShape();
         try {
            Thread.sleep(1000);
         } catch (Exception e) {
            System.out.print("e ");
         }
         doSynch();
      }
   }
}

Which are true? (Choose all that apply.)

  • A. Compilation fails.
  • B. Elapsed time will be about eight seconds.
  • C. Elapsed time will be about nine seconds.
  • D. Elapsed time will be about 12 seconds.
  • E. Changing doSynch() to be unsynchronized will change elapsed by only a few milliseconds.
  • F. Changing doSynch() to be unsynchronized will change elapsed by 450 or more milliseconds.


B and E are correct.

Note

Even when doSynch() is synchronized, the two run() invocations aren't running against the same Main object.

This code creates two distinct Main objects, so there is no synchronization.




PreviousNext

Related