5

I am debugging a Java Application in NetBeans IDE 8.0.2.

When a Thread.start() method is called I am not able to reach the run() method of that thread (though I put the breakpoints in that method). However, sometimes it is hitting the method but sometimes not.

How can I reach the run() method while debugging?

public class JavaApplication7 {

    public static void main(String[] args) {
        Mailthread1 mt1 = new Mailthread1();
        Thread pt = new Thread(mt1);
        pt.setName("Mailthread");
        pt.start();
    }
}

And the Thread class is :

class Mailthread1 implements Runnable {
    public void run() {
        System.out.println("Cant hit this line");
    }
}
Rajesh
  • 2,135
  • 1
  • 12
  • 14
Anil Kumar
  • 2,521
  • 7
  • 23
  • 40
  • Lots of good advice [here](https://netbeans.org/kb/docs/java/debug-multithreaded.html). – OldCurmudgeon Jul 13 '15 at 12:36
  • This smells like a locked monitor problem, but we need to see a small test case that demonstrates this behaviour to diagnose. If that is problematic, try viewing a dump of all the jvm threads (it will list whether they are blocked or not, and if so on what). – Chris K Jul 13 '15 at 13:07
  • @Shar1er80, added code... – Anil Kumar Jul 13 '15 at 13:07
  • And the Mailthread1 code? – Shar1er80 Jul 13 '15 at 13:08
  • The following guide may help you with putting the example code together; currently it is not self contained.. specifically we cannot run it to reproduce the problem. http://meta.stackexchange.com/questions/22754/sscce-how-to-provide-examples-for-programming-questions – Chris K Jul 13 '15 at 13:10
  • @Shar1er80, Added that now.. – Anil Kumar Jul 13 '15 at 13:10
  • @ChrisK, how to view the dump of JVM Threads.? – Anil Kumar Jul 13 '15 at 13:13
  • @AnilKumar there are a few ways to do it, but a good one for starters is to get the pid of the jvm and run 'jstack ' from the command line – Chris K Jul 13 '15 at 13:14
  • I have run the above example ten times from intellij and been able to stop at the debug line every time. Inorder to run, I modified the example code slightly.. adding 'public static void main' and so forth to make it complete. I suspect that your problem is more subtle. Have you reproduced this problem using the above demo code? – Chris K Jul 13 '15 at 13:16
  • I've pasted your code in netbean 8.0.2 and it works just fine. What version of Java are you working with? – Shar1er80 Jul 13 '15 at 13:17
  • 8.0.2. Actually the code is from application. I copied the piece of code. But i am sure i copied it properly. With this same code, I hit the run method, But now i'm not able to – Anil Kumar Jul 13 '15 at 13:19
  • Try cleaning and rebuilding your project, and if all else fails... Reboot! – Shar1er80 Jul 13 '15 at 13:22
  • Just because the code is from the problem application does not mean that this sample will have the same problem.. you may have an interaction going on with other code that is causing it, or you may have a broken environment. We need to work through the possibilities to narrow it down. – Chris K Jul 13 '15 at 13:22
  • @ChrisK, now this is new application code that i Edited. Even in this case, i can't hit the run() method. Please see the above code – Anil Kumar Jul 13 '15 at 13:26
  • @AnilKumar thank you for the question edits, that helps. I am happy that I can enter a debug on the target line in my environment every time (and from experience over 20 years I always have been able to). Your problem is something else. This is probably a bad installation or a bug in the IDE. Give another IDE or machine a try to see if you get the same problem. – Chris K Jul 13 '15 at 13:29
  • @ChrisK, I will come back with your inputs on this. Thank you for your patience and valuable answers – Anil Kumar Jul 13 '15 at 13:29
  • @AnilKumar you are welcome, we have all had problems like this at one time or another. – Chris K Jul 13 '15 at 13:29
  • Re, _sometimes it is hitting the method but sometimes not_ What does that mean? Are you saying that sometimes you see the message printed out to the console and sometimes not? Or are you saying that sometimes it hits your breakpoint, and sometimes it just prints the message without stopping? – Solomon Slow Jul 13 '15 at 15:07
  • @jameslarge, It is printing every time, but sometimes at this line, 'code' pt.start() even if I Step Into, it is not reaching there – Anil Kumar Jul 14 '15 at 04:31
  • 1
    It is a problem with NetBeans 8.0.2. [link] (https://netbeans.org/bugzilla/show_bug.cgi?id=249304) – Anil Kumar Jul 14 '15 at 04:43
  • 1
    You will never get to the `run()` function by stepping into the `start()` function. The `run()` function is called in a different thread from the one that you are single stepping. – Solomon Slow Jul 14 '15 at 12:28

2 Answers2

1

in the JDWP, there are 3 types of breakpoint: class, method and line.

If your IDE fails to intercept the line breakpoint in the println(), then you can try a method breakpoint on the run() reclaration.

If that fails, there is something out of sync between the byte code and the source. You can try adding lines, breakpointing another line above and below.

Other than that, change IDE and/or change JVM. This should work.

user2023577
  • 1,752
  • 1
  • 12
  • 23
0

You don't have a t.join() nor a sleep in the main branch of your code so the new thread starts in theory but your main method also keeps running and exits. The application terminates before it even has a chance to do something in your other thread and the breakpoint is not reachable.