You are on page 1of 3

7/14/13

Thread (SCJP forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Certification Programmer Certification (SCJP/OCPJP)

Author

Thread
posted 3/24/2000 9:51 AM

Nilesh Parikh Ranch Hand Joined: Mar 14, 2000 Posts: 52

hi all, Does the thread stop running when the program exits the main() method? I think the answer is YES. The code that is attched to the run() method of the thread is the main()method, and it will stop as soon as it finishes it's work Am I right? Alkesh

maha anna Ranch Hand Joined: Jan 31, 2000 Posts: 1467

posted 3/24/2000 1:45 PM

Alkesh, The user threads which are started by the main thread keep on continuing their work even if the main thread ends before them. The JVM exits when there is only daemon threads running, and all other threads finished their work. Here is an example and the output. Don't give more importance to the exact sequence of the output. It varies. But the point to understand here is even if the main thread had finished its work very first, t1,t2 user threads keep running and after all threads done their work, JVM exits. I hope this is what you asked for. regds maha anna
1/3

www.coderanch.com/t/190936/java-programmer-SCJP/certification/Thread

7/14/13

Thread (SCJP forum at JavaRanch)

<pre> class TestThread extends Thread { public void run() { for(int i=0; i<5; i++) { try { Thread.sleep(1000); }catch(Exception e) {} System.out.println(this.getName() + " i="+i); } System.out.println(this.getName() +" ends now"); } } class test { public static void main(String[] args) { TestThread t1 = new TestThread(); TestThread t2 = new TestThread(); t1.start(); t2.start(); System.out.println("main() ends now"); } } output -----main() ends now Thread-0 i=0 Thread-1 i=0 Thread-0 i=1 Thread-1 i=1 Thread-1 i=2 Thread-0 i=2 Thread-0 i=3 Thread-1 i=3 Thread-1 i=4 Thread-1 ends now Thread-0 i=4 Thread-0 ends now </pre> [This message has been edited by maha anna (edited March 24, 2000).]

I agree. Here's the link: http://aspose.com/file-tools

subject: Thread

www.coderanch.com/t/190936/java-programmer-SCJP/certification/Thread

2/3

7/14/13

Thread (SCJP forum at JavaRanch)

Similar Threads Parent Thread stops then does child thread also stop? Q on thread at Jxam Doubt regarding destroy() method of Thread Class Threads and join() how to kill main thread and relative subthread?
All times above are in your local time zone & format.T he current ranch time (not your local time) is Jul 14, 2013 08:40:58 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/190936/java-programmer-SCJP/certification/Thread

3/3

You might also like