aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Threads Doubt?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Threads Doubt??" Watch "Threads Doubt??" New topic
Author

Threads Doubt??

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Source: http://www.danchisholm.net/oct1/topic/section7/threads2.html

What is the significance of System.currentTimeMillis();???
Also, why a synchronised block is used???

class A extends Thread {
public void run() {
synchronized (this) {
try {wait(5000);} catch (InterruptedException ie){}
}}
public static void main(String[] args) {
A a1 = new A();
long startTime = System.currentTimeMillis();
a1.start();
System.out.print(System.currentTimeMillis() - startTime + ",");
try {a1.join(6000);} catch (InterruptedException ie) {}
System.out.print(System.currentTimeMillis() - startTime);
}}
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9304
    
  17

Originally posted by marc weber:

Try removing the synchronization, and see what the compiler error tells you.


I don't think that there will be any compiler error...although I have not compiled it and I am not knowledgeable enough to challenge you marc...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
sumi rankan
Ranch Hand

Joined: Apr 07, 2008
Posts: 46
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 ]
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
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).
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
@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.


Preparing for SCJP 6.0
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads Doubt??