Saturday 15 March 2014

java - Before any thread dies will it call notify for all objects waiting on thread reference -



java - Before any thread dies will it call notify for all objects waiting on thread reference -

please check bellow code work fine though haven't called notify(). question thread phone call notifyall() implicitly before dies ?

class threadb extends thread{ int total; @override public void run() { super.run(); synchronized (this) { for(int i=0; i<100; i++) total += i; } } } public class threada { public static void main(string[] args) throws interruptedexception{ threadb b = new threadb(); b.start(); synchronized (b) { b.wait(); system.out.println("threada.main()"); } } }

yes. javadoc states

as thread terminates this.notifyall method invoked. recommended applications not utilize wait, notify, or notifyall on thread instances.

regarding comment

as comment phone call notifyall() when terminates happens if thread terminated , after called join() method

here's recent implementation of java.lang.thread. checks thread#isalive(). when thread has finished, isalive homecoming false.

the scenario follows: thread terminates, internal isalive flag turned false, , notifyall called on it. other thread calls join on , checks isalive. since returns false, phone call join returns immediately.

java android

No comments:

Post a Comment