• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this JWhiz thread question wrong?

 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. Answer B is incorrect.
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
B is wrong. I have written programs where main spawned another thread that became the main application thread, while the main thread terminated.
Rob
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 479
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thx for your explanation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic