-3

I want to see whether main method has finished execution or not with reference to this question [link] (How can child threads still executes even after if their parent thread dies or terminates? tell me the java code to see that main method has finished execution.Please don't tell me this using daemon threads because I don't wanna use daemon Threads.

Community
  • 1
  • 1
TruePS
  • 493
  • 2
  • 12
  • 33
  • "tell me the java code .." ? tell me your code please. – Ankit Rustagi Mar 04 '14 at 08:04
  • This is the link to my code [link] (http://stackoverflow.com/questions/22163922/how-can-child-threads-still-executes-even-after-if-their-parent-thread-dies-or-t). – TruePS Mar 04 '14 at 08:04
  • 1
    @Ankit Downvoting some question doesn't resolve the problem. – TruePS Mar 04 '14 at 08:05
  • If you look at http://stackoverflow.com/questions/5642802/termination-of-program-on-main-thread-exit evidently the application does NOT terminate after the main function terminates. The way to make sure main thread is the last is to use `join()` on all the other threads from the main thread. This way main only finishes after all the other threads finish. – Karthik T Mar 04 '14 at 08:19
  • There is not a parent-child relationship between threads in a JVM. If you want your main method to wait for other threads you must explicitly code that. – Thorbjørn Ravn Andersen Mar 04 '14 at 08:36

2 Answers2

0

tell me the java code to see that main method has finished execution.

How about this:

public static void main(String[] args) {
    // Do stuff ...
    System.err.println("The main method has finished");
}

If that is not what you want, you need to be more clear and specific with your Question.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • This is the link to my code (http://stackoverflow.com/questions/22163922/how-can-child-threads-still-executes-even-after-if-their-parent-thread-dies-or-t) I wanna see when main method() dies. – TruePS Mar 04 '14 at 08:12
  • @TruePS - Well this will tell you that. – Stephen C Mar 04 '14 at 08:18
  • @ Stephen So you are implying that child threads still keeps running if there parent thread that is main method() dies. – TruePS Mar 04 '14 at 08:20
  • 1
    @TruePS - No. I'm not implying anything. I'm just answering your question. However, what you propose *is a true statement*. – Stephen C Mar 04 '14 at 09:18
  • So that statement is true " child threads still keeps running if there parent thread that is main method() die" – TruePS Mar 04 '14 at 09:33
  • @TruePS - Yes. When the parent terminates, the children do not *automatically* terminate as well. But you don't need to write a whole bunch of code to know that. You could read what the javadocs say about the behaviour of threads ... – Stephen C Mar 04 '14 at 10:23
0

If it is just out of interest, Use a debugger to see all the threads of your program.

And please don't mix up thread models. The article you refer to in the other question is about a threading framework for C++. There even exist different threading models for C/C++ on a single system, so always make sure to read about the one you are actually using.

Axel
  • 13,939
  • 5
  • 50
  • 79
  • I am using Netbeans Could you please tell me how to use a debugger to see all the threads of your program.I have never done it.Thanks in advance. – TruePS Mar 04 '14 at 08:16
  • Learn how to start your program in debug mode, and then learn to set break points – Thorbjørn Ravn Andersen Mar 04 '14 at 08:36
  • 1
    Set a breakpoint int `main()`, then right click your main class and select "Debug As Java Application". You can then open the thread view by selecting "View/Debugging/Threads". – Axel Mar 04 '14 at 08:43