Java Eclipse调试器dosn';我不能用这两条线

Java Eclipse调试器dosn';我不能用这两条线,java,multithreading,Java,Multithreading,如何调试这个?我知道这不是线程安全的,但我只想跟踪逻辑 class Test { public static int count = 0; class CountThread extends Thread { public void run() { System.out.println(Thread.currentThread().getName() + " start"); count++; System.out.println

如何调试这个?我知道这不是线程安全的,但我只想跟踪逻辑

class Test {
public static int count = 0;
class CountThread extends Thread {

    public void run()
    {
        System.out.println(Thread.currentThread().getName() + " start");
        count++;
        System.out.println(Thread.currentThread().getName() + " end");
    }
}  

public void add(){
    CountThread a = new CountThread();
    CountThread b = new CountThread();

            a.start();
    b.start();

            try {
        a.join();
        b.join();
    } catch (InterruptedException ex) {
        ex.printStackTrace();
    }

}
public static void main(String[] args) {

    Test test = new Test();
    System.out.println("START = " + Test.count);

            test.add();

            System.out.println("END: Account balance = " + Test.count);
}
当它被执行到a.start()时,在Eclipse中我单击“单步执行”,它不会转到run()方法,而是转到下面,在我的代码中没有办法转到run()。如何调试这个

     public synchronized void start() {
    /**
 * This method is not invoked for the main method thread or "system"
 * group threads created/set up by the VM. Any new functionality added 
 * to this method in the future may have to also be added to the VM.
 *
 * A zero status value corresponds to state "NEW".
     */
    if (threadStatus != 0 || this != me)
        throw new IllegalThreadStateException();
    group.add(this);
    start0();
    if (stopBeforeStart) {
    stop0(throwableFromStop);
}
}
当它被执行到.start()时,在Eclipse中,我单击“单步执行”,它不会转到run()方法,而是转到[Thread.start()方法],在我的代码中无法转到run()

这是意料之中的,Eclipse没有什么问题。当线程分叉并开始执行时,无法调试到正在执行的本机方法中

如何调试这个

     public synchronized void start() {
    /**
 * This method is not invoked for the main method thread or "system"
 * group threads created/set up by the VM. Any new functionality added 
 * to this method in the future may have to also be added to the VM.
 *
 * A zero status value corresponds to state "NEW".
     */
    if (threadStatus != 0 || this != me)
        throw new IllegalThreadStateException();
    group.add(this);
    start0();
    if (stopBeforeStart) {
    stop0(throwableFromStop);
}
}
我会在你的
run()
方法的第一行放一个断点。或者,如果必须,可以在
Thread.run()
方法中放置断点,在该方法中调用
run()
方法:

// the Thread.run() method
public void run() {
    if (target != null) {
        // Your `CountThread` objects are the `target`.
        target.run();
    }
}
注意:您需要了解,此类程序的任何调试都将极大地改变线程的执行顺序。
System.out.println()
调用也可以这样做,因为它们生成IO,并且底层的
PrintStream
同步的。这意味着,一旦删除了print语句和断点,您的程序将表现得非常不同

当它被执行到.start()时,在Eclipse中,我单击“单步执行”,它不会转到run()方法,而是转到[Thread.start()方法],在我的代码中无法转到run()

这是意料之中的,Eclipse没有什么问题。当线程分叉并开始执行时,无法调试到正在执行的本机方法中

如何调试这个

     public synchronized void start() {
    /**
 * This method is not invoked for the main method thread or "system"
 * group threads created/set up by the VM. Any new functionality added 
 * to this method in the future may have to also be added to the VM.
 *
 * A zero status value corresponds to state "NEW".
     */
    if (threadStatus != 0 || this != me)
        throw new IllegalThreadStateException();
    group.add(this);
    start0();
    if (stopBeforeStart) {
    stop0(throwableFromStop);
}
}
我会在你的
run()
方法的第一行放一个断点。或者,如果必须,可以在
Thread.run()
方法中放置断点,在该方法中调用
run()
方法:

// the Thread.run() method
public void run() {
    if (target != null) {
        // Your `CountThread` objects are the `target`.
        target.run();
    }
}
注意:您需要了解,此类程序的任何调试都将极大地改变线程的执行顺序。
System.out.println()
调用也可以这样做,因为它们生成IO,并且底层的
PrintStream
同步的。这意味着,一旦删除了打印语句和断点,程序的行为将非常不同。

start()
不是
run()

您需要在
run()
中设置断点。然后,运行应用程序,eclipse将保持run()

在调试视图中,您将看到两个线程

不要忘记在
a.join()处设置断点也是。

start()
不是
run()

您需要在
run()
中设置断点。然后,运行应用程序,eclipse将保持run()

在调试视图中,您将看到两个线程


不要忘记在
a.join()处设置断点也是。

在运行方法中放置断点如何?在运行方法中放置断点如何?同时设置a.join()的重要性是什么?在所有其他线程消失时继续调试。你有三个线程,如果你在run()中设置了BP,那么你只会捕获其中的两个线程,但是第三个线程(主应用程序线程)会跑掉:)嗯,主线程不会跑掉,因为它会在
连接上被阻塞。
:-)。这是有问题的。当我同时在run()和a.join()中设置时。它首先直接转到a.join(),而不是run()的内部。这正常吗?不应该,没关系。如果您跳过它,它将等待.join()。当线程A完成时,它将再次在主线程中升起,您将跨过它,主线程将再次等待,当线程B完成时,主线程将再次升起。没什么复杂的:)顺便说一句,如果线程B先完成,则流将稍有变化-取决于您首先“跳过”的线程(您可以在“调试”视图中选择该线程)。设置a.join()的重要性是什么?要在所有其他线程都离开时继续调试。你有三个线程,如果你在run()中设置了BP,那么你只会捕获其中的两个线程,但是第三个线程(主应用程序线程)会跑掉:)嗯,主线程不会跑掉,因为它会在
连接上被阻塞。
:-)。这是有问题的。当我同时在run()和a.join()中设置时。它首先直接转到a.join(),而不是run()的内部。这正常吗?不应该,没关系。如果您跳过它,它将等待.join()。当线程A完成时,它将再次在主线程中升起,您将跨过它,主线程将再次等待,当线程B完成时,主线程将再次升起。没什么复杂的:)顺便说一句,如果线程B先完成,那么流将略有变化-取决于您先“跳过”的线程(您可以在“调试”视图中选择线程)。为什么需要同步println()?这就是
PrintStream
的工作方式。这意味着它可以被缓冲,并且多个线程可以写入它,而它们的输出不会被混合。为什么需要println()的同步?这就是
PrintStream
的工作原理。这意味着它可以被缓冲,并且多个线程可以写入它,而不会使它们的输出混杂在一起。