java - How to implement a spinlock to avoid blocking -
consider next code:
// below block executed thread t1 synchronized(obj) { obj.wait(0); } // block executed thread t2 synchronized(obj) { obj.notify(); } i understand in above code if t1 has taken ownership of synchronized block , @ same time if thread t2 tries take synchronized block, t2 goes kernel wait. want avoid situation , spin t2 before block until t1 calls wait , leaves ownership of block. possible?
the jvm need not implement entry locked synchronized block hard block , context switch. has alternative of using lighter weight methods, such spin locks. in fact, oracle jvm goes lengths avoid blocking. might find jvm has done optimisation you. , if has not, might because jvm has evidence spin locks bad idea.
java multithreading thread-synchronization
No comments:
Post a Comment