-4

I have a java program which prints out the last statement of main program and hang. I would like to know what could cause this problem thanks.

 public static void main(String[] args) {

        ...
        ...
        JSch jsch=new JSch();
    try {

        jsch.addIdentity("xxx");
        session = jsch.getSession("centos", this.ip, 22);
        session.setConfig("StrictHostKeyChecking", "no");
        session.connect();

    } catch (JSchException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ...
    ...
        System.out.println("end of test");
    }

the program prints "end of test" and hang. what could cause this problem? I didnt use any framework. I compiled and run this in linux machine. after printing the "end of test", the linux prompt ">" doesnt show up

from ps command, I see the process is running.

i found the problem. the session conection doesnt get disconected. but why it blocks the exit? correct me if i am wrong. there is no join or wait inside main thread. can main thread get blocked?

Answer: with Stephen C 's help, finally, i found the reason. in c++, all threads are terminated after main thread exit, but in java, JVM wait for all non-Daemon thread to complete. I applied my c++ knowledge to Java but they are different in this case.

cppython
  • 1,209
  • 3
  • 20
  • 30
  • 4
    Those `...` dots are likely hide some critical code. Please post your [mcve]. – Hovercraft Full Of Eels Nov 25 '15 at 22:18
  • 1
    Something is making your program hang, and it isn't the print-statement, so you need to provide more information about your code to be able to get help. – RaminS Nov 25 '15 at 22:18
  • 2
    "*the program prints "end of test" and hang*" are you sure it hangs? Maybe it simply ended? Or maybe you created some additional thread which didn't end yet preventing your app to exit. – Pshemo Nov 25 '15 at 22:18
  • 1
    What do you mean it hangs? Are you running it from the command line command prompt? Please improve the question. Have you tried stripping out the rest of the code and running just what you have shown here? Does it still hang with the simplified version? – Noah Nov 25 '15 at 22:20
  • Does it still show up in the list of running processes? `ps -ef | grep java` – Noah Nov 25 '15 at 22:29
  • 1
    So, are you going to show us enough code to allow us to answer this question? Or are you going to delete the question? it's really one or the other. – Hovercraft Full Of Eels Nov 25 '15 at 22:33
  • @HovercraftFullOfEels, there are alot of code, I can't show here. I just want to get some hints – cppython Nov 25 '15 at 22:35
  • @HovercraftFullOfEels - Actually, he >>WANTS<< guesses and conjectures. So he is not wasting >>HIS<< time by asking. – Stephen C Nov 25 '15 at 22:38
  • @StephenC: I suppose you're right, but one of the main purposes of this site is not to answer personal private questions, but to ask common problems that everyone can benefit from, and that includes putting effort into the question so that it is answerable without guessing, something he's refusing to do. He's hiding key threading code for no apparent reason. Yes, he has a lot of code, so he should do some debugging **first** before coming here and try to isolate the involved code. He knows it must involve threading so where's his effort in this? It's really a lazy question. – Hovercraft Full Of Eels Nov 25 '15 at 22:40
  • And besides, asking for just "hints" by definition puts the question in the "too broad" category. – Hovercraft Full Of Eels Nov 25 '15 at 22:43
  • 1
    @cppython "*people is asking me for source code. but there are a lot, i dont think you want to read it*" we are not asking you to post your full code. Take your time and create [Short, Self Contained, Correct (Compilable), Example](http://www.sscce.org/). There are even high chances that you will find cause of your problem while creating such example. – Pshemo Nov 25 '15 at 22:55

2 Answers2

3

One possibility is that you have created another thread, and that thread hasn't terminated yet. The JVM won't shut down of its own volition until all (non-daemon) threads have terminated.

Another possibility is that you have implemented a shutdown hook, and the hook has blocked.

From ps command, I see the process is running and single thread.

That evidence is consistent with either of the above, I think.


Can we troubleshoot from outside (such as ps command) instead of source code.

Try using jstack (I think it is called) to get a thread stack dump.

Try attaching a debugger to the JVM.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • ps -axfj. it shows no child under this process. I believe there is no other thread – cppython Nov 25 '15 at 22:39
  • The "main" thread has terminated ... so you could/would be seeing the other thread. Note that a shutdown hook is a thread too. Now, if your program is single-threaded and doesn't use shutdown hooks, then you can dismiss these possibilities ... but they >>are<< consistent with the evidence that you have presented so far. – Stephen C Nov 25 '15 at 22:41
  • 23357 23378 23357 20112 pts/1 23357 Sl+ 1000 0:02 | \_ /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.65-2.b17.el7_1.x86_64/jre/bin/java -classpath /vagrant/aws-java-sdk-1.10.32/third-party/aspectj-1.6/aspectjrt.jar:... ... CreateSecurityGroupApp – cppython Nov 25 '15 at 22:48
  • How do you know that is the "main" thread? All of the threads would look like that. – Stephen C Nov 25 '15 at 22:49
  • based on ppid, if parent thread is dead, all child will attach to init process, therefore, the ppid should be 1. but this process 's ppid is not 1 – cppython Nov 25 '15 at 22:51
  • Erm, really? Are you confusing thread behavior with process behavior? Native threads don't have parent threads, do they? – Stephen C Nov 25 '15 at 22:52
  • can you give me a link to read "Native threads don't have parent threads" – cppython Nov 25 '15 at 23:04
  • one thing, I try "ps -T -p 23378" command. it shows a lot of java – cppython Nov 25 '15 at 23:07
  • 23378 23378 pts/1 00:00:00 java 23378 23383 pts/1 00:00:00 java 23378 23384 pts/1 00:00:00 java 23378 23385 pts/1 00:00:00 java 23378 23386 pts/1 00:00:00 java 23378 23387 pts/1 00:00:00 java 23378 23388 pts/1 00:00:00 java 23378 23389 pts/1 00:00:00 java 23378 23390 pts/1 00:00:00 java 23378 23391 pts/1 00:00:00 java 23378 23392 pts/1 00:00:00 java 23378 23393 pts/1 00:00:00 java 23378 23394 pts/1 00:00:00 java 23378 23395 pts/1 00:00:00 java 23378 23396 pts/1 00:00:00 java 23378 23397 pts/1 00:00:00 java 23378 23398 pts/1 00:00:00 java – cppython Nov 25 '15 at 23:08
  • http://stackoverflow.com/questions/4666628/do-child-threads-exit-when-the-parent-thread-terminates from this post, it says the main thread must exist – cppython Nov 25 '15 at 23:13
  • i just narrow down my problem. Could you read my new post and give me some hints again. thank you so much. I will keep update something I found – cppython Nov 26 '15 at 00:40
  • I don't have time, but I'd bet that the problem is that the session has some threads associated with them and you need to "disconnect" to make the threads go away. Did you use "jstack"? – Stephen C Nov 26 '15 at 01:39
  • 1
    Yes ... there is an answer that says that the when the "main" thread terminates, then the JVM exits. Unfortunately, it is incorrect. And it is easy to prove that it is incorrect. – Stephen C Nov 26 '15 at 01:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/96211/discussion-between-cppython-and-stephen-c). – cppython Nov 26 '15 at 07:22
  • Ah ... I see. That question is actually about C++. C++ is a different language, and threads behave differently. Basically, that Q&A is irrelevant to Java thread behavior – Stephen C Nov 26 '15 at 07:37
  • found another link http://stackoverflow.com/questions/5642802/termination-of-program-on-main-thread-exit – cppython Nov 26 '15 at 07:53
  • finally, i found the reason. in c++, all threads are terminated after main thread exit, but in java, JVM wait for all non-Daemon thread to complete. I applied my c++ knowledge to Java. – cppython Nov 26 '15 at 08:03
0

You need to explicitly call session.disconnect(); and also if you using channel then you need to discount channel too.

taras
  • 6,566
  • 10
  • 39
  • 50
Mehboob K
  • 1
  • 1