1

My code:

package multithreading;

    public class JoinT1T2T3 extends Thread  
    {
        public void run()
        {
            if(Thread.currentThread().isDaemon())
            {
                System.out.println("Daemon thread is running");
            }
            else
            {
                System.out.println("Slave thread");
            }
        }


        public static void main(String args[])
        {


            Thread t=new Thread();
            t.setDaemon(true);
            T1 thread1=new T1();
            T2 thread2=new T2();
            T3 thread3=new T3();
            System.out.println("First Thread name is::: "+thread1.getName());
            thread1.setName("XXXXXXXXXX");
            System.out.println("First Thread After changing name::: "+thread1.getName());
            System.out.println("First thread's id is :::"+thread1.getId());
            thread1.start();



        try
        {

                thread1.join(3000);
            }
            catch(Exception e)
            {
                System.out.println("-----------");
            }
            //thread2.start();
            thread2.start();
            thread3.start();


        }
    }

Why is my daemon not reachable? Please provide me an explanation and code.

T1,T2,T3 are three different classes. The main method is also the run method. I created a new thread instance. Still getting "code not reachable". Which is if{} else{} in run method.

Simon Prickett
  • 3,838
  • 1
  • 13
  • 26
ben
  • 19
  • 1
  • 1
    you have never called t.start(). the thread is never started. Also, you have to extend thread or provide a Runnable instance to t.start() – RuntimeException Aug 12 '15 at 13:48
  • "Why is my deamon not rachable, please provide explanation with code." Can you explain what you mean? – Peter Lawrey Aug 12 '15 at 13:54
  • Hello Peter Lawrey, I was asking why the sop in run() method is not printed to console? What shall i do test how a deamon thread works. I'd appreciate if you can provide me a good simple example illustrating how a deamon thread works OR a code which depicts the behavior of deamon thread. Thank you. – ben Aug 14 '15 at 13:44

1 Answers1

0

You are getting the "not reachable" error because JoinT1T2T3#run() is never called anywhere.


To Reach JoinT1T2T3#run()

In order for a run() method to be called, an object of its containing class should be explicitly passed to a Thread at construction-time using Thread#(Runnable). For example:

Thread t = new Thread(new JoinT1T2T3());

Now, Thread t will call JoinT1T2T3#run() whenever it starts.


To Run t

Even if t is a daemon, it must still be start()'ed just like any other Thread, or else it will not run. (and thus JoinT1T2T3#run() will never be called) So you need to do this:

Thread t = new Thread(new JoinT1T2T3());
t.setDaemon(true);
t.start();

explaining "daemon" threads

Generally speaking, in Java the only functional difference between a deamon Thread and a non-daemon Thread is that "the Java Virtual Machine exits when the only threads running are all daemon threads" (source). Other than that, a daemon Thread behaves exactly like a non-daemon Thread, syntax and all. It must still be constructed with a Runnable and start()'ed, just like any other Thread.

However, because a daemon thread may be abruptly terminated when the JVM exits, daemons should be used sparingly. In particular, daemons should not be used for any I/O or resource cleanup. "Normal" guarantees about stuff like finally blocks and stack unwinding don't necessarily apply to daemon threads--if the JVM decides to exit, all daemons are immediately abandoned. (source)

Java daemons should be used only for background housekeeping, bookkeeping, metrics, non-critical monitoring, etc.

Community
  • 1
  • 1
Travis
  • 2,135
  • 17
  • 29