Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Preempting the main thread

 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is it possible for the main thread or the intial thread to be preempted while its children are not?
Thanks
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you mean. To me in order for a thread to be pre-empted the thread must have been running in the first place. Only one thread can run at a time. So if a thread is pre-empted, no other threads are pre-empted at the same time, since no other threads were actually running.
That probably isn't what you meant, so can you rephrase?
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the confussion.
What I meant is something like this:

When I type java A, a main thread is created. When the program reaches line 1, a child thread is created. So now there are two threads, the main and the child.
My question now is can the main thread be swapped-out (that is moved out from the running state) but its child remains running?
Thanks.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the way the JVM deals with threads, it doesn't really care about the parent-child relationship. The JVM just keeps running as long as there are non-daemon threads still running. It starts with one thread, main, starts another thread (call it thread-1), mean ends, but the JVM doesn't care, it just knows that it's still got one (non-daemon) thread running, so it keeps on going until that ends (assuming no other threads are spawned).
Does that answer your question, or did I misunderstand again?
 
Alton Hernandez
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that answer it Thank you.
I was thinking along the line on how most OS swap programs out.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic