File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A thread problem

 
sabah ammar
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi pals,
i got this question
public class PrintJ extends Thread{
int j=0;
public void run(){
try{
Thread.sleep(5000);
}catch(Exception e){
}
j=100;
}
public static void main(String []a){
PrintJ t1=new PrintJ();
t1.start();
System.out.println(t1.j);
}}
Q.What you have to do to ensure that j will print 100?
A.you have to make t1 as Daemon Thread.
B.you have to join t1 to main.
C.value of j is set to 100;
D.you have to interupt the main method.

Please describe the answer to me which accordingly is option B.thank you very much.
 
Joyce Lee
Ranch Hand
Posts: 1392
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sabah,

To make sure 100 (not 0) is printed, join() of t1 must be called before System.out.println statement.



By calling t1.join(), it suspends the current thread, i.e. the main thread in this case, until t1 thread is completed.

Joyce
[ February 26, 2005: Message edited by: Joyce Lee ]
 
sabah ammar
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
exactly, main was suspended in other words....am very worried about threads for the exam...
anyways, thank you very much joyce .
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic