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() + " ");
}
}
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.
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.
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.
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 -
run()
method along with the main()
method.t.start()
.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").
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With