Results 1 to 3 of 3
- 01-20-2010, 02:10 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
- 01-20-2010, 09:04 PM #2
in a command line application, such as when we launch from the static void main(String[] args), when the main thread exits, that effectively causes the VM to start doing its shutdown..
Daemon threads are really only good in applets (i think), to allow the thread to run when the [applet] is otherwise finished running, beacuse typically the lifespan of the Java vm in a web browser plugin, is the duration the browser is open, not the duration the user is browsing the page containing the applet.
so for the command line applications, to cause it to wait until a child thread finishes, we need a piece of busy waiting logic in the main thread
Java Code:public static void main(String[] args) { // your thread, of your implementation of runnable Thread childThread = new Thread(new Runnable() { public void run() { for(int i = 0; i < 10; i++) { System.out.println("in child thread"); try { Thread.sleep(1000); } catch (InterruptedException ex) {} } } }); System.out.println("main thread is starting child thread."); childThread.start(); System.out.println("main thread is done, now just waiting for child thread to finish."); while (childThread.isAlive()) { // sleep for some small amount of time to prevent consuming cpu cycles in a tight while loop try { Thread.sleep(20); } catch (InterruptedException ex) {} } System.out.println("child thread has exited. now main thread exiting too"); } // main
- 01-27-2010, 01:43 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 10
- Rep Power
- 0
I Think, This is you Question , "Parent Thread waits until child finish its work".
Join() helps you!...
Just Use
try
{
childThread.join(); // Parent waits until the ChildThread dead
}
catch(InterruptedException ie) //should catch Interrupted Exception
{
System.out.println(ie.getMessage());
}
//main Ends...
Similar Threads
-
Child-Class Calling a Method in a Parent-Class
By Blah_ in forum New To JavaReplies: 5Last Post: 09-29-2009, 02:48 AM -
change url in parent window from child window
By rakesh_n_mehta in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 07-09-2009, 12:17 PM -
[SOLVED] How to pass information from child class to parent class
By pellebye in forum New To JavaReplies: 7Last Post: 05-06-2009, 12:42 PM -
Parent & Child window issues......
By jithan in forum New To JavaReplies: 2Last Post: 09-20-2008, 09:21 AM -
passing a value from parent thread to child thread
By sachinj13 in forum Threads and SynchronizationReplies: 7Last Post: 09-07-2008, 09:06 PM


LinkBack URL
About LinkBacks

Bookmarks