• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

about Thread  RSS feed

 
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I got it from http://www.danchisholm.net/july21/topic/section7/threads2.html

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();
System.out.println(startTime);

a1.start();
System.out.print(System.currentTimeMillis() - startTime + ",");
try {a1.join(6000);} catch (InterruptedException ie) {}
System.out.print(System.currentTimeMillis() - startTime);
}}

Answer:
1.The first number printed is greater than or equal to 0
2.The second number printed must always be greater than 5000

Really confusing me. It would be a great help if anyone explain this?

Thanks in advance

Preparing Scjp1.5
 
Author
Posts: 3418
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The above line will print the current time in milliseconds, which is greater than or equal to zero.




A new thread is spawned from the main thread, which waits for atleast 5 seconds. wait(5000). So the second number printed should be always greater than 5000. The wait method can only guarantee a minimu of 5 seconds. It could take 5.2 seconds. The a1.join(6000) blocks for a1 to die or minimum 6000 milliseconds elapses, which ever happens first. In the above example, a1 should die in atleast 5 seconds.
 
Preethi Dev
Ranch Hand
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks ,I am clear now.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!