This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
F. Changing doSynch() to be unsynchronized will change elapsed by 450 or more
The answer is :
Answer (for Objective 4.3):
B and E are correct. Even when doSynch() is synchronized, the two run() invocations
aren’t running against the same Checkout2 object. This code creates two distinct
Checkout2 objects, so there is no synchronization.
A, C, D, and F are incorrect based on the above.
My interpretation is that t1.join means the t1 thread will execute at a certain time and the CPU allocate its resources for t1 until it is done and the other thread will wait until t1 is done.
Therefore, I think the time elapse can be 8 second/thread * 2 thread = 16 seconds. Think of a scenario that t1 execute first and it takes 8 seconds and the other thread execute next and takes another 8 second.
I thought the answer B is not guaranteed.
Joined: Nov 01, 2011
I thought about this question myself for a while. I guess the two threads can sleep at the same time and wake up at the same or more or less the same time.