Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The quession for Thread.sleep(times)

 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class s extends Thread{int j=0;
public void run() {
try{Thread.sleep(5000);}
catch(Exception e){}
j=100;
}
public static void main(String args[])
{
s t1=new s();
t1.start();
System.out.println(t1.j);
}
}
what you have to do to ensure that 'j' will print 100
a you have make t1 as Daemon Thread
b You have join the t1 to main
c You have to suspend the main when the thread starts and resume it after
d the value of 'j' is set to 100
e You have to interrupt the main thread
Ans is "b", Why? and why not "c"
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hades
Suspend and resume are both deprecated methods. Using join you can ensure the main thread does not continue running until after the t1 has completed.
hope that helps

------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Hades Pan
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it thanks a lot~~!
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic