Net-informations.com
SiteMap  | About    

static synchronization vs non-static synchronization

Java supports multiple threads to be executed. This may cause two or more threads to access the same fields or objects concurrently. Synchronization is the way used to protect access to resources that are accessed concurrently. A synchronized block of code can only be executed by one thread at a time.

Synchronization in Java is basically an implementation of monitors . When synchronizing a non static method, the monitor belongs to the instance. When synchronizing on a static method , the monitor belongs to the class. In case of non-static synchronized method memory is allocated multiple time whenever method is calling. But memory for static method is allocated only once at the time of class loading. That means while execution of a static method the whole class is blocked. So other static synchronized methods are also blocked. If one thread is executing a static synchronized method, all other threads trying to execute any static synchronized methods will be blocked.







net-informations.com (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.