Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

eclipse java multithread program debugging

Tags:

scjp

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() + " "); 
    }
}
like image 531
rama Avatar asked Aug 19 '13 13:08

rama


People also ask

How do I debug a multithreaded program in Eclipse?

Start debug session. When the breakpoint in run is hit, you can go to another breakpoint, enable that breakpoint if it was disabled. Then right click on the breakpoint -> go to Filters, now you can select the thread you want the breakpoint to be remain enabled for and you can uncheck the rest of the threads.

Why it is difficult to debug multi threaded programs?

Parallel processing using many threads can greatly improve program performance, but it may also make debugging more difficult because you're tracking many threads. Multithreading can introduce new types of potential bugs.

How do you program a multithread in Java?

We create a class that extends the java. This class overrides the run() method available in the Thread class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the Thread object.


1 Answers

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.

like image 112
Awesome Avatar answered Sep 30 '22 03:09

Awesome