Total members 11895 |It is currently Mon Dec 23, 2024 9:35 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





Daemon term is mainly used in UNIX. In Java, this is used to indicate a special type of thread. Normally when a thread is created in Java, by default it is a non-daemon thread. Whenever a Java Program is executed, the Java Virtual Machine (JVM) will not exit until any non-daemon threads are still running. This means if the main thread of an application ends and the remaining threads left are Daemon threads, then the JVM will exit killing all the daemon threads without warning.
A daemon thread should be used for some background task that might provide a service to the applications. e.g. a Server thread listening on a port for the clients` requests. A thread for which an exit method is not provided or which does not have an exit mechanism can be marked as a daemon thread.

Note that the normal C++ code is like this :
Code:
#include <iostream>
#include <pthread.h>

using namespace std;

#define threadCount     6

void *myThreadFunc(void *threadArg)
{
   long numberOfThread;
   numberOfThread = (long)threadArg;
   cout << "  Thread number now is:" << numberOfThread << endl;
   pthread_exit(NULL);
}

int main ()
{
   pthread_t threadArray[threadCount];
   int status;
   int i;
   for( i=0; i < threadCount; i++ ){
      cout << "main()  - starting thread: #" << i << endl;
      status = pthread_create(&threadArray[i], NULL, 
                          myThreadFunc
, (void *)i);
      if (status){
         cout << "error in creating a thread ," << status << endl;
         exit(-1);
      }
   }
   pthread_exit(NULL);
}
 


with output:
Code:
main()  - starting thread: # 0
main()  - starting thread: # 1
main()  - starting thread: # 2
main()  - starting thread: # 3
main()  - starting thread: # 4
main()  - starting thread: # 5
Thread number now is: 0
Thread number now is: 1
Thread number now is: 2
Thread number now is: 3
Thread number now is: 4
Thread number now is: 5




_________________
Please recommend my post if you found it helpful


Author:
Beginner
User avatar Posts: 95
Have thanks: 2 time

thank you for this information . :)

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time

Daemon term is mainly used in UNIX. In Java, this is used to indicate a special type of thread. Normally when a thread is created in Java, by default it is a non-daemon thread. Whenever a Java Program is executed, the Java Virtual Machine (JVM) will not exit until any non-daemon threads are still running. This means if the main thread of an application ends and the remaining threads left are Daemon threads, then the JVM will exit killing all the daemon threads without warning.


Author:
Newbie
User avatar Posts: 9
Have thanks: 0 time

thank you this is a good addaition .

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 4 posts ] 

  Related Posts  to : daemon thread - What is the use of deamon thread?
 Thread Safe     -  
 invokes a thread's run() method     -  
 Thread of Event Dispatcher     -  
 Multi Thread Program     -  
 initial state of thread     -  
 high-level thread states     -  
 thread cannot acquire a lock on an object     -  
 thread state when it terminates its processing     -  
 Plz Help,I want BOTH THREAD to work at a time in SAME WINDOW     -  



Topic Tags

C++ Threads
cron





Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com