0

I have 3 threads called T1 , T2 and T3 and also i have 3 daemon threads like dt1, dt2 and dt3.

I want to (assign) provide a service dt1 to thread T1 , dt2 to thread T2 and dt3 to thread T3. when threads T1,T2 and T3 complete their runnable task it's related daemon thread also got closed internally.

Can any one please tell me how to do it in java using thread daemon concept?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Do you mean something like a `callback`? http://stackoverflow.com/questions/826212/java-executors-how-to-be-notified-without-blocking-when-a-task-completes – Stefan Falk Jan 10 '14 at 12:34
  • thanks stefan , i don't know much about executor framework, i am asking is there any way to assign a daemon thread to specific thread (If i have multiple threads and multiple daemons , each daemon belongs to perticular thread). for example if a thread wants to behave like a thread we are calling isDaemon(boolean). but i want to assign this daemon thread to perticular thread in java. – Rajasekhar Burepalli Jan 10 '14 at 12:47
  • What do you mean with "assign"? Do you mean `join()`? – Stefan Falk Jan 10 '14 at 13:02
  • yes, Just like that only. when we call a join() on any thread so it will go to waiting state until to complete that thread. so in this case i have a doubt the nature of daemon thread will destroy after all threads gets completed or assigned (joined) thread completed? – Rajasekhar Burepalli Jan 10 '14 at 13:08

2 Answers2

0

"Daemon thread" is not a concept - it's just a feature of Java threads. When a JVM is terminating, it waits for non-daemon threads to terminate by themselves. On the contrary, daemon threads just get terminated no matter what they're doing.

Leaving that behind, one idea might be to establish a "shutdown" flag inside your "daemon" thread. When the non-daemon thread terminates, it could set that flag to true. The daemon thread would check the flag and terminate once it's true. Remember to synchronize that mechanism properly, e.g. by using the volatile keyword.

Bartosz Klimek
  • 555
  • 2
  • 5
0

So, if I understand your question correctly, you want each 'work thread' T1..T3 to have its own background thread (dt1…dt3) doing some co-processing, and you want the background thread to exit when your main thread exits, yes? You could do something like this:

Make each 'main thread T1… a Runnable that looks like this, so that when you launch your T1, it launches its own dt1, and then asks it to shutdown (via interrupt()) when it finishes.

   @Override
   public void run() {
      ExecutorService e = Executors.newSingleThreadExecutor();
      Runnable r = new Runnable() {

         @Override
         public void run() {
            // this is your deamon thread
            boolean done = false;
            while (!done && !Thread.currentThread().isInterrupted()){
               // Do your background deamon stuff here.
               //
               // Try not to use blocking, uninterruptible methods.
               //
               /* If you call a method that throws an interrupted exception,
                * you need to catch that exception and ignore it (or set done true,)
                * so the loop will terminate. If you have other exit conditions, 
                * just set done=true in this body */
            }
         }
      };
      e.execute(r);  // launch your daemon thread

      try {
         // do your main stuff here 
      }
      finally {
         e.shutdownNow(); // this will 'interrupt' your daemon thread when run() exits.
      } 
   }
JVMATL
  • 2,064
  • 15
  • 25
  • thanks JVMATL for sharing information, you understood my question exactly but in that you explained using executor framework, i need concrete thread without executor framework i need threads setDeamon() and join(). In that explanation you are used anonymous runnable task how jvm knows this is a deamon (anonymous ) thread. Please share the information. – Rajasekhar Burepalli Jan 13 '14 at 05:14
  • @RajasekharBurepalli Hi Raj! Help us understand a little more about the context of your problem: (1) Why, exactly, do you feel that you need Daemon Threads specifically? The main reason people use Daemon threads is they don't stop the JVM from exiting if they are still running -- but you want to make sure your 'Daemons' shut down cleanly, so there must be some other reason you need (or think you need) Daemons - can you explain? (2) Also: you say you 'need' setDaemon() and join() instead of the executor framework - what if you could accomplish the same effect with executors - would that be ok? – JVMATL Jan 13 '14 at 15:24
  • @RajasekharBurepalli (I should also point out that, although my example used an anonymous runnable, you can submit your own Runnable to an executor. (It is important, however, not to confuse the runnable with the thread it runs in; the same runnable could be run in a Daemon thread and a non-daemon thread during its lifetime. Thread objects can only be started once once, but Runnables can be run over and over.) – JVMATL Jan 13 '14 at 15:33