Results 1 to 2 of 2
  1. #1
    frejon26 is offline Member
    Join Date
    Jan 2008
    Posts
    14
    Rep Power
    0

    Default main thread question?

    are there any problems that arise from letting the main thread finish execution before sibling threads finish. I know the thread.join() method prevents this when you have a reference to the thread object running.

    on my computer it does not seem to casue any visible problems.

    -thanks
    Last edited by frejon26; 01-24-2008 at 10:18 PM.

  2. #2
    roots's Avatar
    roots is offline Moderator
    Join Date
    Jan 2008
    Location
    Dallas
    Posts
    293
    Rep Power
    7

    Default

    Java Doc Thread Class and Thread.setDaemon() and this link might help you.

    I played with this code
    Java Code:
    import java.io.FileNotFoundException;
    import java.io.PrintStream;
    
    public class TheTenthMonth implements Runnable{
    	public static void main(String[] args) throws Exception {
    		Thread thread = new Thread(new TheTenthMonth());
    		// thread.setDaemon(true);
    		thread.start();
    		
    		System.out.println("I am already pregnant");	
    		// System.exit(1); // This makes difference
    	}
    
    	@Override
    	public void run() {
    		try {
    			Thread.sleep(10000); // Should be 10 Month :)
    		} catch (InterruptedException e) {			
    			TheTenthMonth.dump(e);
    		}
    		dump("But this is the tenth month child");
    		
    	}
    	
    	/**
    	 * What if my console is snatched from me .. Does not happen but to make sure..
    	 */
    	public static void dump(Object o){
    		System.out.println(o);
    		PrintStream out;
    		try {
    			out = new PrintStream(Math.random()+".txt");
    			out.print(o);
    		} catch (FileNotFoundException e) {
    			e.printStackTrace();
    		}
    		
    	}
    }
    dont worry newbie, we got you covered.

Similar Threads

  1. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 11:30 PM
  2. Replies: 1
    Last Post: 08-07-2007, 08:10 AM
  3. Replies: 1
    Last Post: 07-27-2007, 11:59 PM
  4. Replies: 1
    Last Post: 07-16-2007, 07:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •