aspose file tools*
The moose likes Beginning Java and the fly likes two threads always active in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "two threads always active in java" Watch "two threads always active in java" New topic
Author

two threads always active in java

nirjari patel
Ranch Hand

Joined: Apr 23, 2009
Posts: 374
I was question in an interview "what two threads are always active in java"

main() is the thread which is always active. Whiuch is the other one ? Is it garbage collection ? Is garbage collection thread available in all the programs O(I mean , if there are 4 programs running on OS, will each program have its own java thread) OR is it just one one thread provided by JVM to all the java programs running on OS i.e. no matter how many java programs are running on OS, there will just be one gc thread doing gc for all these programs ?

Thanks
Matthew Cox
Greenhorn

Joined: May 25, 2009
Posts: 29
The Main thread (I don't know the given name for it, if any) and the Event Dispatch thread. As the name implies, all Event Driven callbacks occur on this thread. It's a big one used in GUI development (for example).
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
Assuming the application has no GUI, could the other answer be the main thread and the garbage collector? I would assume you'd have at least one garbage collector for each JVM process. (In other words, using Windows as an example, for each instance of the java.exe/javaw.exe process, one garbage collector). The Event dispatching thread would only be present in those applications which make use of Swing/AWT in which case it would be a 3rd thread. The garbage collection thread would always be running, GUI app or not.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

It's a fundamentally flawed question, as standard JVMs have half-a-dozen or more threads running on startup, and that's not even counting the GUI event thread. Depending on which GC implementation is running, garbage collection can take as few as one or as many as three or more threads all by itself. There's also a Finalizer thread, a "Reference Handler" thread, a "Signal Dispatcher" thread, a "Low Memory Detector" thread, and the main thread, of course. There may also be Hotspot compiler threads!

I would imagine that the simple-minded "the main thread and the GC thread" is what the interviewer was looking for.


[Jess in Action][AskingGoodQuestions]
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
++What Ernest said.
Every time I'm using Eclipse to debug an application there are always way more than 2 active threads.
Matthew Cox
Greenhorn

Joined: May 25, 2009
Posts: 29
Alex Hurtt wrote:Assuming the application has no GUI, could the other answer be the main thread and the garbage collector? I would assume you'd have at least one garbage collector for each JVM process. (In other words, using Windows as an example, for each instance of the java.exe/javaw.exe process, one garbage collector).



Hmmmm I definitely would not be willing to repudiate your suggestion since I have spent a lot more of my time in the recent years working with C#. However, my intuition tells me that GC may be a part of the main thread. Garbage Collection isn't a process that is going on at all times. Objects are marked for GC but could take a long amount of time to actually be GCed (relative to processing speed). Therefore, I would not be inclined to suggest that GC would need it's own thread by default (that isn't to say that GC isn't executed on a asynchrnous thread started by the main thread). Many possibilities here.

I did a little reading on the Thread class in the Java 5.0 API
When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:


This is interesting considering the question. However, it doesn't prove that there isn't a second thread of a daemon type.

Exerpt from Oracle's Threading Tutoria: http://download.oracle.com/javase/tutorial/essential/concurrency/procthread.html

But from the application programmer's point of view, you start with just one thread, called the main thread. This thread has the ability to create additional threads, as we'll demonstrate in the next section.

Matthew Cox
Greenhorn

Joined: May 25, 2009
Posts: 29
Ernest Friedman-Hill wrote:It's a fundamentally flawed question, as standard JVMs have half-a-dozen or more threads running on startup, and that's not even counting the GUI event thread. Depending on which GC implementation is running, garbage collection can take as few as one or as many as three or more threads all by itself. There's also a Finalizer thread, a "Reference Handler" thread, a "Signal Dispatcher" thread, a "Low Memory Detector" thread, and the main thread, of course. There may also be Hotspot compiler threads!

I would imagine that the simple-minded "the main thread and the GC thread" is what the interviewer was looking for.


I definitely agree with this statement. I just found this tidbit.

Every application has at least one thread — or several, if you count "system" threads that do things like memory management and signal handling.


 
It is sorta covered in the JavaRanch Style Guide.
 
subject: two threads always active in java