Originally posted by Abhi vijay: ... What is the significance of System.currentTimeMillis();??? Also, why a synchronised block is used??? ...
startTime is set to currentTimeMillis(). So after that, currentTimeMillis() - startTime shows how much time has passed since startTime was set. This is a way to demonstrate that the thread is waiting for 5000 milliseconds.
Try removing the synchronization, and see what the compiler error tells you.
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer sscce.org
There is no compiler error but a runtime exception(IllegalMonitorStateException) thrown.. because wait() notify() notifyAll() must be called from within a synchronized context....meaning the thread should be owner of the lock of the object on which these methods are called. [ October 15, 2008: Message edited by: sumi rankan ]
Joined: Sep 16, 2008
The output is: The first number is greater than or equal to 0. The second is greater than 5000.
But how can the first no be greater than or equal to 0??? and when synchronized keyword is not used, then I get runtime error(illegalMonitorStateException).
@Abhi Vijay: The second print statement may print a value less than 5000 also.
@Ankit: The program would give IllegalMonitorStateException since wait,notify should be called from synchronized context. Sumi is correct in this case.
@Abhi Vijay: The first print statement shows a zero because by the time the second thread starts, the main thread already goes on to next statement after the a1.start() and it executes the line: System.out.print(System.currentTimeMillis() - startTime + ","); and since then, there is no difference between CurrentTime and startTime, it prints zero.