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.)
doSynch()
to be unsynchronized will change elapsed by only a few milliseconds.doSynch()
to be unsynchronized will change elapsed by 450 or more milliseconds.B and E are correct.
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.