File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Preempting the main thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Preempting the main thread" Watch "Preempting the main thread" New topic
Author

Preempting the main thread

Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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

Joined: Jan 30, 2000
Posts: 18671
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?


"I'm not back." - Bill Harding, Twister
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443
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

Joined: Jan 30, 2000
Posts: 18671
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

Joined: May 30, 2003
Posts: 443
Yes, that answer it Thank you.
I was thinking along the line on how most OS swap programs out.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Preempting the main thread
 
Similar Threads
Main Method
Defining threads
java.lang.NoSuchMethodError
main thread
Unclear