• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Thread question

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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:
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for answers !
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic