Wednesday 15 September 2010

java - Guarded blocks -- notifyAll() vs interrupt() -



java - Guarded blocks -- notifyAll() vs interrupt() -

this q looks verification and/or comments/opinions following:

the illustration on guarded blocks follows:

public synchronized void guardedjoy() { // guard loops 1 time each special event, may not // event we're waiting for. while(!joy) { seek { wait(); } grab (interruptedexception e) {} } system.out.println("joy , efficiency have been achieved!"); }

the other end of code-- 1 setting joy this:

public void setjoy2(theclass t) { synchronized (t) { t.joy = true; t.notifyall(); } }

the above "signaling" on joy utilize of notify().

an alternative managing "signalling" interrupt():

public void guardedjoy2() { // guard loops 1 time each special event, may not // event we're waiting for. while(!joy) { synchronized(this) { seek { wait(); } grab (interruptedexception e) {} } } system.out.println("joy , efficiency have been achieved!"); }

and 1 setting joy , letting thread waiting is:

public void setjoy2(theclass t) { t.joy = true; t.interrupt(); }

i'm looking create comparing between two-- setjoy() , setjoy2().

first of all, guardedjoy2() above can "hear" both setjoy() , setjoy2() properly-- can see when joy set , deed way expected to(?) how guardedjoy2() compare guardedjoy()? achieves same thing guardedjoy()-- might missing something, i'm not seeing difference in outcome. difference guardedjoy2() released lock of this within loop, , else acquire before method terminates unexpected results. setting aside (i.e., assuming place utilize of joy , side effects appear in code), there's not difference between guardedjoy() , guardedjoy2()(?)

guardedjoy2() responds both setjoy() , setjoy2(). can "hear" setjoy() when done, re-acquires lock , go there. and, can "hear" setjoy2()-- receiving interrupt , throwing interruptedexception out of wait(), that's end of synch'd statement, checks see in while status joy set , goes there. if interrupt "someone" else , not 1 setting joy, gets loop 1 time again same way till joy set.

when, wait() invoked , lock of this released in guardedjoy2(), other thread can in acquiring lock , things not supposed done till joy set , guardedjoy2() supposed homecoming properly. however, setting aside (again, assuming isn't issue-- thing beingness looked seeing message on lastly line of guardedjoy2() on console.) this-- setjoy2() can preferable in cases other things can done on object while it's getting joy set , go there (in setjoy2(), thread setting joy doesn't have have lock of object interrupt while setjoy() should have lock invoke notifyall() on it).

how guardedjoy2() & setjoy2() compare guardedjoy() & setjoy() above?

tia.

i'm going of assumption meant notify() in first setjoy rather notifyall().

first, it's of import note if you're invoking interrupt() on look of type theclass, theclass subclass of thread. goes against number of -recommendations state should utilize runnable instances encapsulate logic run on thread rather subclassing class thread. javadoc of thread#join(int) states

it recommended applications not utilize wait, notify, or notifyall on thread instances.

this because implementations of java utilize methods handle thread logic behind scenes. if not know implementation logic , utilize these methods thread instances, might undesired behavior.

then, , might warrant profiling, throwing (creating) exception expensive operation, more removing thread object's wait set. what's more, exceptions should used exceptional conditions, not guide application logic.

i going sec example's synchronization order may wrong (assuming joy not volatile) because read in guardedjoy2 loop might not see write in setjoy2. however, java language specification states

if thread t1 interrupts thread t2, interrupt t1 synchronizes-with point other thread (including t2) determines t2 has been interrupted (by having interruptedexception thrown or invoking thread.interrupted or thread.isinterrupted)

so still have visibility guarantees in place.

java multithreading concurrency synchronization locking

No comments:

Post a Comment