wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is this JWhiz thread question wrong? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Is this JWhiz thread question wrong?" Watch "Is this JWhiz thread question wrong?" New topic
Author

Is this JWhiz thread question wrong?

Rick Reumann
Ranch Hand

Joined: Apr 03, 2001
Posts: 281
Which of the following is true regarding main Thread?
A) It is the thread from which other "child" threads will be spawned.
B) It must be the last thread to finish execution. When the main thread stops, the program terminates.
C) It has the highest priority.
D) main is not a thread.
Answer: A, B
I knew answer A but then I was lost. Why must the main thread be the last thread to finish execution? I thought unless a spawned thread was set as a Daemon thread, that the main thread could terminate but any other spawned threads could keep going?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
You are correct. Answer B is incorrect.


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
B is wrong. I have written programs where main spawned another thread that became the main application thread, while the main thread terminated.
Rob


Rob
SCJP 1.4
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
I am not sure of that. Can a spawned thread die after the main. I don't think it. Could you please explain with some code???


Younes
By constantly trying one ends up succeeding. Thus: the more one fails the more one has a chance to succeed.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Thx for you reply, but I am not sure to come to the same conclusion.
This is the output from your code

in main
in new Thread

Does it mean that the main thread had died? Why?
If you change your code to this.

Here is the output:
in main
in main
in main
in new Thread// this is line 674 of the output
in main
in main
in main
in main
There is something that I must have not catch, because I don't understand your conclusion.
Do you have an url where it is explained???
Again thx a lot
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Originally posted by Younes Essouabni:
I am not sure of that. Can a spawned thread die after the main. I don't think it. Could you please explain with some code???

Yes.

Rob
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9052
    
  12
Originally posted by Younes Essouabni:
I am not sure of that. Can a spawned thread die after the main. I don't think it. Could you please explain with some code???

A spawned thread may die before or after the main thread. They are independent. The program goes on until the last non-daemon thread is finished, regardless whether it is the main thread or a spawned thread.
Younes Essouabni
Ranch Hand

Joined: Jan 13, 2002
Posts: 479
Thx for your explanation.
 
Don't get me started about those stupid light bulbs.
 
subject: Is this JWhiz thread question wrong?