• Post Reply Bookmark Topic Watch Topic
  • New Topic

about Thread  RSS feed

 
Preethi Dev
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
 
arulk pillai
Author
Ranch Hand
Posts: 3413
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!