| Author |
A thread problem
|
sabah ammar
Greenhorn
Joined: Jan 10, 2005
Posts: 23
|
|
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
Joined: Jul 11, 2003
Posts: 1392
|
|
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
Joined: Jan 10, 2005
Posts: 23
|
|
exactly, main was suspended in other words....am very worried about threads for the exam... anyways, thank you very much joyce .
|
 |
 |
|
|
subject: A thread problem
|
|
|