-
class MainClass{
-
public static void main(String a[]){
-
//do something
-
}
-
}
-
When i run this class file then JVM is the process having one main thread.
Thread ends and process ends.
I went through a link there i read about daemon thread.
"a daemon thread is one which won't prevent the program from ending." Basically it means main program/main thread. -
class HelloThread extends Thread
-
{
-
public void run()
-
{ try{
-
for ( ; ; )
-
{
-
System.out.println("hello");
-
sleep(1000);
-
} }catch(Exception e){}
-
}
-
}
-
public class ThreadTest
-
{
-
public static void main(String[] args) throws Exception
-
{
-
Thread hello = new HelloThread();
-
hello.start();
-
System.out.println("Sorry, I must be leaving");
-
}
-
}
-
That means here HelloThread is non-daemon. Here main program finishes but HelloThread still running. So all the threads are by default non-daemon?
13 9245 JosAH 11,448
Recognized Expert MVP
Near the start of the Thread API documentation:
Each thread may or may not also be marked as a daemon. When code running in some thread creates a new Thread object, the new thread has its priority initially set equal to the priority of the creating thread, and is a daemon thread if and only if the creating thread is a daemon.
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).
kind regards,
Jos
Well ....
I have some definitions of threads. Daemon means Devil(violates the normal actions). Is it? Daemon Thread: It won't prevent program from ending. That means if the wants to stop and then Daemon Thread won't stop it from ending.
Anything wrong in my conception here?
Here the term program, what does it mean? Please clarify me!
In my case the New Thread is Daemon or Non-Daemon and Why?
Which parts of the quoted text above do you not understand?
You don't know what a program is??
The second code...Main thread creating a New Thread.
Here program means .. the creating thread. In my previous example Main Thread?
See the quoted text above. Do you see the word "program" there?
Yeah seen.... But here program means the Main Thread which calls the main method?
JosAH 11,448
Recognized Expert MVP @dmjpro
I had hoped that the little quote had tickled your curiosity; if you had read the API documentation, you'd have seen the next sentence:
The Java Virtual Machine continues to execute threads until either of the following occurs:
* The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
* All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.
The main thread is not a daemon thread and when it dies and no other non-daemon threads are alive the entire jvm will shut down.
kind regards,
Jos
You are no longer making much sense. Just read the quoted text above and stick with terminology used in that text.
But the thing is in my example the New Thread is Daemon or Non-Daemon?
As Josh said if no Non-Daemon threads alive then JVM shuts down. If the creating thread is Daemon then new threads will be Daemon. Is it applicable for Non-Daemon? i think .. no ;)
Another tutorial says .... Daemon thread is the service providers for other threads.
Here other threads mean both or Non-Daemon only? So "if no Non-Daemon threads alive then JVM shuts down" this line valid. If there no Non-Daemon threads then to whom service to be provided.
Up to this is cleared so far (having some confusion what i asked).
Now come to my program... The main thread creates the new thread which is simply running an infinite loop. Now here main thread ends but the new thread still running. If the set the Daemon flag true using setDaemon(true) then new thread end up before the main thread. So the new thread is a non-daemon as it changes the behaviour after changing the daemon flag to true. In which sense the new thread is called Non-Daemon?
JosAH 11,448
Recognized Expert MVP
There is nothing special about deamon threads or non-daemon threads. A (non)daemon thread creates (non)daemon threads. The status of a thread can be changed with the setDaemon(...) call before a thread is started.
If no non-daemon threads are alive anymore the JVM will quit. That is all there is to it.
kind regards,
Jos
JosAH 11,448
Recognized Expert MVP @r035198x
I know; that's what my mother already said ages ago ;-)
kind regards,
Jos
You snuck your response between mine and dmjpro's just to make me look silly.
JosAH 11,448
Recognized Expert MVP @r035198x
I didn't do it! I'm innocent! One of my parrots walked over my keyboard; naughty parrot! No cookie! Naughty!
kind regards,
Jos :-D
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: klaus triendl |
last post by:
hi,
recently i discovered a memory leak in our code; after some investigation i
could reduce it to the following problem:
return objects of functions are handled as temporary objects, hence...
|
by: Mario |
last post by:
Hello,
I couldn't find a solution to the following problem (tried
google and dejanews), maybe I'm using the wrong keywords?
Is there a way to open a file (a linux fifo pipe actually) in...
|
by: Yves Glodt |
last post by:
Hello,
if I do this:
for row in sqlsth:
________pkcolumns.append(row.strip())
________etc
without a prior:
|
by: Adrian Herscu |
last post by:
Hi all,
In which circumstances it is appropriate to declare methods as non-virtual?
Thanx,
Adrian.
|
by: Bern McCarty |
last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains
other than the default AppDomain? Is there any means at all of doing this?
Mixed-mode is incredibly convenient, but if I...
| |
by: ypjofficial |
last post by:
Hello All,
So far I have been reading that in case of a polymorphic class ( having
at least one virtual function in it), the virtual function call get
resolved at run time and during that the...
|
by: Ian825 |
last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
|
by: amitvps |
last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
|
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= |
last post by:
PEP 1 specifies that PEP authors need to collect feedback from the
community. As the author of PEP 3131, I'd like to encourage comments
to the PEP included below, either here (comp.lang.python), or...
|
by: puzzlecracker |
last post by:
is it even possible or/and there is a better alternative to accept
input in a nonblocking manner?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |