1
package test1;

public class ThreadA extends Thread {
    private ThreadB b;

    public ThreadA(ThreadB b) {
        super();
        this.b = b;
    }

    @Override
    public void run() {
        try {
            synchronized (b) {
                b.start();
                System.out.println("first run !");
                b.wait();
                System.out.println("ThreadB destory after ThreadA auto notify");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

package test1;

public class ThreadB extends Thread {
    @Override
    public void run() {
        try {
            System.out.println("b run begin timer=" + System.currentTimeMillis());
            Thread.sleep(6000);
            System.out.println("b run   end timer=" + System.currentTimeMillis());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

package test1;

public class Run {
    public static void main(String[] args) {
        ThreadB b = new ThreadB();
        b.setName("B");
        ThreadA a = new ThreadA(b);
        a.setName("A");
        a.start();
    }
}

print result: first run ! b run begin timer=1587441580267 b run end timer=1587441586267 ThreadB destory after ThreadA auto notify

why ThreadB destroy after ThreadA auto notify??

i want know hotspotVM level answer。

thank you !

startjava
  • 33
  • 5
  • i want know hotspotVM source evel implements,i think threadB destroy do some thing. – startjava Apr 21 '20 at 04:04
  • As pointed out by one of the duplicates, when a thread dies its `this.notifyAll()` method is invoked. This is documented by [`Thread#join(long)`](https://docs.oracle.com/en/java/javase/14/docs/api/java.base/java/lang/Thread.html#join(long)). If you're really interested in seeing the source code then you can browse the [online repository](https://hg.openjdk.java.net/jdk). I can't be more specific as I don't know exactly where this is implemented. – Slaw Apr 21 '20 at 05:21

0 Answers0