eclipse java multithread program debugging
Asked Answered
B

1

5

while debugging the java Multithreading program i put breakpoints. after start method is invoking the control is not going to run menthod can you please let me know the debug procedure.

sample code

class Test extends Thread { 
    public static void main(String[] args) { 
        try { 
            Thread t = new Thread(new Test());
            t.start(); 
            t.start(); 
        } catch (Exception e) { 
             System.out.print("e "); 
        } 
    } 

    public void run() { 
        for(int i = 0; i < 2; i++) 
            System.out.print(Thread.currentThread().getName() + " "); 
    }
}
Bulger answered 19/8, 2013 at 13:11 Comment(1)
Please be more explicit or provide some code.Cristicristian
A
8

Debugger starts with main thread, since your breakpoint is in main thread.
t.start() spawns a new thread.
But the debugger will continue with the main thread itself.

If you want to debug the newly created thread, then you have to set a breakpoint in run() method also.Then the debugger control goes to the newly created thread, but it is invisible to the user.

If you want to see the control in run() method of newly created thread, then you have to follow the below steps -

  1. Put a breakpoint in run() method along with the main() method.
  2. Start debugging the program till you hit the statement t.start().
  3. After completing t.start(), go to "Debug" view. There you will find 2 threads running.(You can find the "Debug" view in eclipse by going to "Window -> Show View -> Debug").
    • First one is main thread
    • Second one is newly created thread (e.g. [Thread-1] )
  4. Click on the second thread to see the control in run method.
  5. After completion of your thread execution, go to the "Debug" view again and click on the main thread to continue with the main thread debugging.

Note: If you continue with the main thread after 3rd step towards the end of the thread, then you will not be able to debug your new thread.

Awlwort answered 18/9, 2013 at 14:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.