Mixing Static As Well As Not Static Synchronized Method - Coffee Error 2
Using static in addition to non static synchronized method for protecting shared resources is or in addition to thus other Java error nosotros are going to verbalize over inwards this business office of our serial “learning from mistakes inwards Java”. In concluding article nosotros induce got seen why double in addition to float should non move used for monetary calculation , In this tutorial nosotros volition abide by out why using static in addition to non static synchronized method together for protecting same shared resources is non advisable.
Further Learning
Multithreading in addition to Parallel Computing inwards Java
Java Concurrency inwards Practice - The Book
When to utilization Thread or Runnable interface inwards Java?
I induce got seen or in addition to thus times Java programmer mix static synchronized method in addition to illustration synchronized method to protect same shared resource. They either don't know or failed to realize that static synchronized in addition to non static synchronized method lock on 2 dissimilar object which breaks move of synchronizing shared resources every bit 2 thread tin concurrently execute these 2 method breaking usual exclusive access, which tin corrupt status of mutable object or fifty-fifty drive subtle race status inwards Java or fifty-fifty to a greater extent than horrible deadlock inwards java.
Static in addition to non static synchronized method Java
static synchronized method locked on cast object e.g. for string cast its String.class while illustration synchronized method locks on electrical flow illustration of Object denoted past times “this” keyword inwards Java. Since both of these object are dissimilar they induce got dissimilar lock in addition to thus piece 1 thread is executing static synchronized method , other thread inwards java doesn’t demand to aspect for that thread to provide instead it volition acquire carve upwardly lock denoted byte .class literal in addition to move inwards into static synchronized method. This is fifty-fifty a popular multi-threading interview questions where interviewer asked on which lock a detail method gets locked, or in addition to thus fourth dimension likewise seem inwards Java testify papers.
Bottom occupation is that never mix static in addition to non static synchronized method for protecting same resource.
Example of Mixing illustration in addition to static synchronized methods
Here is an example of multithreading code which is using static in addition to non static synchronized method to protect same shared resource:
here shared count is non accessed inwards usual exclusive fashion which may consequence inwards passing wrong count to caller of getCount() piece or in addition to thus other thread is incrementing count using static increment() method.
That’s all on this business office of learning from mistakes inwards Java. Now nosotros know that static in addition to non static synchronized method are locked on dissimilar locks in addition to should non move used to protect same shared object.
Further Learning
Multithreading in addition to Parallel Computing inwards Java
Java Concurrency inwards Practice - The Book
When to utilization Thread or Runnable interface inwards Java?
0 Response to "Mixing Static As Well As Not Static Synchronized Method - Coffee Error 2"
Post a Comment