Mixing Static In Addition To Not Static Synchronized Method - Coffee Error 2

Using static too non static synchronized method for protecting shared resources is or too then other Java error nosotros are going to speak over inwards this role of  our serial “learning from mistakes inwards Java”. In final article nosotros stimulate got seen why double too float should non travel used for monetary calculation , In this tutorial nosotros volition uncovering out why using static too non static synchronized method together for protecting same shared resources is non advisable.

I stimulate got seen or too then times Java  programmer mix static synchronized method too event synchronized method to protect same shared resource. They either don't know or failed to realize that static synchronized too non static synchronized method lock on 2 dissimilar object which breaks purpose of synchronizing shared resources equally 2 thread tin concurrently execute these 2 method breaking usual exclusive access, which tin corrupt status of mutable object or fifty-fifty crusade subtle race status inwards Java or fifty-fifty to a greater extent than horrible deadlock inwards java.


Static too non static synchronized method Java

Using static too non static synchronized method Mixing static too non static synchronized method - Java error 2static synchronized method locked on degree object e.g. for string degree its String.class while event synchronized method locks on electrical flow event of Object denoted yesteryear “this” keyword inwards Java. Since both of these object are dissimilar they stimulate got dissimilar lock too then piece i thread is executing static synchronized method , other thread inwards java doesn’t demand to facial expression for that thread to supply instead it volition acquire split upwards lock denoted byte .class literal too motility into 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 too then fourth dimension besides seem inwards Java essay papers.

Bottom work is that  never mix static too non static synchronized method for protecting same resource.


Example of Mixing event too static synchronized methods

Here is an example of multithreading code which is using static too non static synchronized method to protect same shared resource:

public class SynchornizationMistakes {
    private static int count = 0;
 
    //locking on this object lock
    public synchronized int getCount(){
        return count;
    }
 
    //locking on .class object lock
    public static synchronized void increment(){
        count++;
    }
   
}

here shared count is non accessed inwards usual exclusive fashion which may effect inwards passing wrong count to caller of getCount() piece or too then other thread is incrementing count using static increment() method.

That’s all on this role of learning from mistakes inwards Java. Now nosotros know that static too non static synchronized method are locked on dissimilar locks too should non travel used to protect same shared object.

Further Learning
Multithreading too Parallel Computing inwards Java
Java Concurrency inwards Practice - The Book
When to utilisation Thread or Runnable interface inwards Java?

Sumber https://javarevisited.blogspot.com/

0 Response to "Mixing Static In Addition To Not Static Synchronized Method - Coffee Error 2"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel