• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Threads Doubt??

 
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic