<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[JavaRanch: Latest posts for the topic "Parent Thread should wait till all child thread are dead"]]></title>
		<link>http://www.coderanch.com/forums/t/27/Threads-Synchronization/Parent-Thread-should-wait-till</link>
		<description><![CDATA[Latest messages posted in the topic "Parent Thread should wait till all child thread are dead"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Parent Thread should wait till all child thread are dead</title>
				<description><![CDATA[I have a Parent Class which spawns 10 child threads,<br />  <br /> class Parent{<br />  <br /> public static void main(String[] args) {<br />  <br /> int threadWorkerNo = 10;<br /> processStartTime = System.currentTimeMillis();<br /> spawnThreads(threadWorkerNo);<br />  <br /> System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");<br />  <br /> } <br />  <br /> private static void spawnThreads(int number){<br />  <br /> for(int i=1;i&lt;=number; i++){<br /> String name = Integer.toString(i);<br /> MigrationWorker th = new MigrationWorker(name);<br /> th.startThread();<br /> }<br />  <br /> }<br /> }<br />  <br /> Assume the MigrationWorker is a Thread Class which does some work.<br /> now i want the Parent class main method to wait till all the 10 migration workers thread are died.<br /> That means when all the 10 child threads are dead,&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&   will be printed.<br />  <br /> Thanks in advance....<br />  <br /> [ April 09, 2007: Message edited by: Purujit Saha ]<br /> [ April 09, 2007: Message edited by: Purujit Saha ]<br /> ]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/233745/1088091</guid>
				<link>http://www.coderanch.com/forums/posts/preList/233745/1088091</link>
				<pubDate><![CDATA[Mon, Apr 9 2007 02:16:00 MDT]]></pubDate>
				<author><![CDATA[Purujit Saha]]></author>
			</item>
			<item>
				<title>Parent Thread should wait till all child thread are dead</title>
				<description><![CDATA[You can use the following code to do the needful.<br />  <br /> <pre>  package test;
 
import java.util.concurrent.atomic.AtomicInteger;
 
public class DeathBed {
 
    public static void main(String[] args) {
        Object monitor = new Object();
        Cremator cremator = new Cremator(10, monitor);
        for(int i=0; i &lt; 10; i++) {
            MyThread myThread = new MyThread(cremator);
            myThread.start();
        }
        synchronized(monitor) {
            try {
                monitor.wait();
                System.out.println(&quot;Time for cremation !!!!!!!&quot;);
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
 
    private static class Cremator {
 
        private AtomicInteger deadThreads;
        private int maxThreads;
        private Object monitor;
 
        public Cremator(int maxThreads, Object monitor) {
            this.maxThreads = maxThreads;
            this.monitor = monitor;
            deadThreads = new AtomicInteger();
        }
 
        public void increment() {
            int i = deadThreads.incrementAndGet();
            System.out.println(&quot;Number of dead threads: &quot; + i);
            if(i &gt;= maxThreads) {
                synchronized(monitor) {
                    monitor.notifyAll();
                }
            }
        }
    }
 
    private static class MyThread extends Thread {
        private Cremator cremator;
 
        public MyThread(Cremator cremator) {
            this.cremator = cremator;
        }
 
        public void run() {
            double v = Math.random();
            try {
                Thread.sleep((long) (v * 10 * 1000));
                cremator.increment();
            } catch(InterruptedException e) {
                e.printStackTrace();
            }
        }
 
    }
}
  </pre><br />  <br /> P.S: MyThread is just a dummy Implementation. It will be replaced by the actual thread that does the work you want to do.<br /> [ April 09, 2007: Message edited by: Nitesh Kant ]<br /> ]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/233745/1088092</guid>
				<link>http://www.coderanch.com/forums/posts/preList/233745/1088092</link>
				<pubDate><![CDATA[Mon, Apr 9 2007 03:56:00 MDT]]></pubDate>
				<author><![CDATA[Nitesh Kant]]></author>
			</item>
			<item>
				<title>Parent Thread should wait till all child thread are dead</title>
				<description><![CDATA[If you have JDK 1.5 or later then this should also work.  There are also ways to do this with queued executor services (using Futures) if you want real thread pool management (please note the comments concerning queued executor services).<br /> <pre>    
public static void main(String[] argv)
{
MigrationWorker[] tasks = getMigrationWorkers();

// The ExecutorService must immediately begin execution of the tasks,
// TASKS MUST NOT BE QUEUED !!
ExecutorService executor = Executors.newCachedThreadPool();

// submit all tasks to be executed
for(MigrationWorker task: tasks)
executor.submit(task);

// immediately shutdown the executor, this will NOT shutdown the executor threads
// but it will prevent execution of worker tasks that have not started. hence the
// requirement that the executor service immediately execute rather than queue tasks
// for later execution
executor.shutdown();
try
{
// wait for the running tasks to complete
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS);
} 
catch (InterruptedException x)
{
// if this occurs then its probably best to just loop and await some more
x.printStackTrace();
}
}
System.out.println(&quot;This should always print last&quot;);

 
</pre>]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/233745/1088093</guid>
				<link>http://www.coderanch.com/forums/posts/preList/233745/1088093</link>
				<pubDate><![CDATA[Mon, Apr 9 2007 06:00:00 MDT]]></pubDate>
				<author><![CDATA[Chris Beckey]]></author>
			</item>
			<item>
				<title>Parent Thread should wait till all child thread are dead</title>
				<description><![CDATA[Your spawnThreads() method doesn't save any of the thread objects for the threads that it created. If it did (such as saving the objects in a collection which is returned from the method), you can later have your main thread call join() on all the thread objects, to wait for them all to complete.<br />  <br /> Henry]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/233745/1088094</guid>
				<link>http://www.coderanch.com/forums/posts/preList/233745/1088094</link>
				<pubDate><![CDATA[Mon, Apr 9 2007 06:05:00 MDT]]></pubDate>
				<author><![CDATA[Henry Wong]]></author>
			</item>
			<item>
				<title>Parent Thread should wait till all child thread are dead</title>
				<description><![CDATA[<blockquote class="uncited">
			<div>Originally posted by Henry Wong:<br /> Your spawnThreads() method doesn't save any of the thread objects for the threads that it created. If it did (such as saving the objects in a collection which is returned from the method), you can later have your main thread call join() on all the thread objects, to wait for them all to complete.<br />  <br /> Henry</div>
		</blockquote><br />  <br /> Henry, firstly i am delighted to communicate with the author of the book i learned Threads from !!!<br /> Keeping aside the requirement in this problem, is it a good idea to use a wait-notify mechanism against the join paradigm? <br /> One scenario can be when threads are not computation intensive and finish their work pretty soon. So, will it be more effecient to use a wait-notify mechanism over "joining". The reason being that the parent will unnecessarily try to join even if the worker thread has already finished its job?<br /> Worth mentioning, its just a hypothetical situation for academic reasons!]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/233745/1088095</guid>
				<link>http://www.coderanch.com/forums/posts/preList/233745/1088095</link>
				<pubDate><![CDATA[Mon, Apr 9 2007 06:17:00 MDT]]></pubDate>
				<author><![CDATA[Nitesh Kant]]></author>
			</item>
			<item>
				<title>Parent Thread should wait till all child thread are dead</title>
				<description><![CDATA[join() is, in fact, implemented in <a href="http://www.javaranch.com" class="faq" title="A Friendly Place for Java Greenhorns" target="_new">Java</a> code using wait() and notify(). The first example is just a terribly complex way of implementing Henry's suggestion by hand. If I saw code like that in a review, I'd make the author change it to use join() in a loop.]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/233745/1088096</guid>
				<link>http://www.coderanch.com/forums/posts/preList/233745/1088096</link>
				<pubDate><![CDATA[Mon, Apr 9 2007 07:45:00 MDT]]></pubDate>
				<author><![CDATA[Ernest Friedman-Hill]]></author>
			</item>
			<item>
				<title>Parent Thread should wait till all child thread are dead</title>
				<description><![CDATA[Point well taken. Tx for the comment]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/233745/1088097</guid>
				<link>http://www.coderanch.com/forums/posts/preList/233745/1088097</link>
				<pubDate><![CDATA[Mon, Apr 9 2007 22:23:00 MDT]]></pubDate>
				<author><![CDATA[Nitesh Kant]]></author>
			</item>
			<item>
				<title>Parent Thread should wait till all child thread are dead</title>
				<description><![CDATA[All of your inputs has helped me to solve my problem.<br /> Thanks to all.]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/233745/1088098</guid>
				<link>http://www.coderanch.com/forums/posts/preList/233745/1088098</link>
				<pubDate><![CDATA[Thu, Apr 12 2007 03:50:00 MDT]]></pubDate>
				<author><![CDATA[Purujit Saha]]></author>
			</item>
			<item>
				<title>Parent Thread should wait till all child thread are dead</title>
				<description><![CDATA[Thanks All !!<br /> <br /> It solved my problem too.]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/233745/2076419</guid>
				<link>http://www.coderanch.com/forums/posts/preList/233745/2076419</link>
				<pubDate><![CDATA[Wed, Sep 30 2009 16:10:15 MDT]]></pubDate>
				<author><![CDATA[Shiaber Shaam]]></author>
			</item>
	</channel>
</rss>
