File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Do different types of Threads exist 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 "Do different types of Threads exist in JAVA?" Watch "Do different types of Threads exist in JAVA?" New topic
Author

Do different types of Threads exist in JAVA?

Abhishek Dhar
Greenhorn

Joined: Nov 07, 2006
Posts: 16
Hello friends i have some problem related to threads.......

I have once gone through a site there i read that there ar 2 types of threads.

DEMON THREAD.
USER DEFINED THREAD.




It was a online samples of technical Q asked in interviews MNC like Google, Accenture, Cognizent etc

I have gone through various books like

1. A Programmer's Guide To Java Certification, 2nd Edition (Addison & Khalid).

2. Head First JAVA, 2 Edition (Kethy Sierra & Bert Bates).


In these books there is nothing written about the Types of Threads.
Do really the types of threads exist, if yes try to explan there definition.


Regards From ---- @bhishek Dh@r
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40

Check out the JavaDocs for the Thread class, there are methods that will report whether a thread is a Deamon thread or not (user thread). You may also set whether a thread is a daemon thread or not -- and you can do this at anytime.

In terms of thread behavior, there is no real difference between between a user thread and daemon thread. It is more of a JVM behavior. Think of it like this: Daemon threads are for the purpose of serving user threads, so if there are no user threads running in the JVM, the JVM will exit, even if there are still Daemon threads running.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39084
    
  23
Please don't post the same question twice.

[edit]This is what I said on the other post on the closed version of this thread.

Yes, there are two kinds of thread. Look through the Thread class in the API, and use ctrl-F then daemon.

A non-daemon thread (user thread) keeps running. If there are any non-daemon threads, the JVM keeps running too, until the last non-daemon thread "dies" (or finishes).

A daemon thread does not keep on running if all the non-daemon threads finish. So if you only have daemon threads still running, then the JVM will not keep running, and the daemon threads stop.

More poetically, from C Horstmann, G Cornell, Core Java 2 7/e vol II Advanced Features, Santa Clara: Sun Microsystems Press (Prentice-Hall) (2005) page 19:- [start quote]

Daemon Threads

You can turn a thread into a daemon thread by calling

t.setDaemon(true);

There is nothing demonic about such a thread. A daemon is simply a thread that has no other role in life than to serve others. Examples are timer threads that send regular "timer ticks" to other threads. When only daemon threads remain, the virtual machine exits. There is no point in keeping the program running if all remaining threads are daemons.[end quote]


[ /edit]
[ April 04, 2007: Message edited by: Campbell Ritchie ]
Abhishek Dhar
Greenhorn

Joined: Nov 07, 2006
Posts: 16
Actually the this question got repeatedly posted because of the internet problem so i did a mistake of posting same thing twice

any way thanks is there any one else who can help me in a more detailed manner
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
What more detail do you want?

There are only two types of thread in Java, daemon and non-daemon, and only one simple difference between them. That difference has been explained here, and is explained in the API.

Presumably, there's something about these explanations that you don't understand. Say what it is and people will I'm sure try to help.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
 
subject: Do different types of Threads exist in JAVA?