Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A thread problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A thread problem" Watch "A thread problem" New topic
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 .
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A thread problem
 
Similar Threads
How to use the Runnable??
a question about thread
The quession for Thread.sleep(times)
Use of Volatile keyword
Clarify Use of the join method