File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread question" Watch "Thread question" New topic
Author

Thread question

Kaspar Minosiantz
Ranch Hand

Joined: Sep 25, 2003
Posts: 36
Hi !
i've been perplexed about a thread life cycle
there is question from preparation to SCJP 1.4
------------------------------------------------------
which of the following is true regarding main Thread
1. It's the thread from which other child threads wil be spawned
2. It must be the last thread to finish execution. When the main thread
stops , the program terminates.
3. It has highest priority
4. main is not a thread .
-------------------------------------------------------
correct answers are 1 & 2
i'm curious about the second answer [ When the main thread stops , the
program terminates. ]
is it realy true ?
Thanks a lot !


SCJP 1.4 SCWCD 1.4 <a href="http://www.jroller.com/page/kaspar_ru" target="_blank" rel="nofollow">blog</a>
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
I'd say the option 2 is correct for a single-threaded application, but not for the one where many threads are started. This code

produces the output of
Main is done!!!
Thread-0
Thread-1

It's not guaranteed on every OS, but you see, main() thread in this case decided to complete first.
KATE MOORE
Greenhorn

Joined: Nov 12, 2003
Posts: 7
[QB]I'd say the option 2 is correct for a single-threaded application, but not for the one where many threads are started.[QB]

Are r sure Vad? Check this..
class MainThread{
public static void main(String s[]){
Thread t1=new Thread(new A());
Thread t2=new Thread(new A());
Thread t = Thread.currentThread();
System.out.println("Current Thread:" +t);
t1.start();
t2.start();
//System.out.println("Main is done!!!");
System.out.println(t.isAlive());
}
}
class A implements Runnable{
public void run(){
try{
Thread.sleep(1000);
System.out.println(Thread.currentThread().getName());
}catch(InterruptedException ie){}
}
}
[ November 13, 2003: Message edited by: KATE MOORE ]
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504

Are r sure Vad? Check this..

isAlive() instance method returns true if the thread is alive. You're assigning the currently executing thread, which is main(), to a reference t. No wonder that when main() flow execution hits

the result is true. The main() is still alive at that point. I hope the following code can clarify some of your doubts:
Sudhakar Krishnamurthy
Ranch Hand

Joined: Jun 02, 2003
Posts: 76
Will the actual exam have such ambigious questions and if so which answer to pick and how do we decide?
Any suggestions
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
You are not likely to see any ambiguously worded questions on the test. Pretty much all of them use a very clear language, so if you're familiar with material, you'll at least understand "what is asked".
Kaspar Minosiantz
Ranch Hand

Joined: Sep 25, 2003
Posts: 36
Thanks a lot for answers !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread question
 
Similar Threads
Thread
When Main Thread Stops ?
2 questions about Threads
JWhiz Thread Question (Pract.6, #51)
Is this JWhiz thread question wrong?