标签:
Java supports multiple threads to be executed. This may cause two or more threads to access the same fields or objects. Synchronization is a process which keeps all concurrent threads in execution to be in synch. Synchronization avoids memory consistence errors caused due to inconsistent view of shared memory. When a method is declared as synchronized; the thread holds the monitor for that method’s object If another thread is executing the synchronized method, your thread is blocked until that thread releases the monitor.
Synchronization in java is achieved using synchronized keyword. You can use synchronized keyword in your class on defined methods or blocks. Keyword can not be used with variables or attributes in class definition.
Object level locking is mechanism when you want to synchronize a non-static method or non-static code block such that only one thread will be able to execute the code block on given instance of the class. This should always be done to make instance level data thread safe. This can be done as below :
理解:对象级别的锁:在给定一个对象的情况下,只有一个线程能够执行代码块。用来保护对象级别数据的线程安全。
public class DemoClass { public synchronized void demoMethod(){} } or public class DemoClass { public void demoMethod(){ synchronized (this) { //other thread safe code } } } or public class DemoClass { private final Object lock = new Object(); public void demoMethod(){ synchronized (lock) { //other thread safe code } } }
Class level locking prevents multiple threads to enter in synchronized block in any of all available instances on runtime. This means if in runtime there are 100 instances of DemoClass, then only one thread will be able to execute demoMethod() in any one of instance at a time, and all other instances will be locked for other threads. This should always be done to make static data thread safe.
理解:类级别锁:避免多个线程进入每一个实例的同步块。假如DemoClass有100个实例,每个实例的demoMethod()方法同一时刻只能有一个线程执行。用来保护类级别数据的线程安全。
public class DemoClass { public synchronized static void demoMethod(){} } or public class DemoClass { public void demoMethod(){ synchronized (DemoClass.class) { //other thread safe code } } } or public class DemoClass { private final static Object lock = new Object(); public void demoMethod(){ synchronized (lock) { //other thread safe code } } }
Let‘s say you have two different instances of the same class, who need to update some static field when they do work. In this case, you will have to use the class to synchronize on, because the static field is shared between instances:
public class Example { private static int n; public void doStuff() { // do some stuff synchronized(Example.class) { n++; } } }
In this code, n keeps track of how often doStuff is called, no matter on which instance it is called on. In order to update n, you need to make sure that no other thread accesses it at the same time. Because n is shared between different instances, you can‘t use synchronized(this), you have to use a lock that is also shared between different instances. In this case, we use Example.class, which we can be quite certain will be shared between all instances of Example.
Note that it doesn‘t matter what lock you use to synchronize on, as long as it is the same lock for all instances. We could also have used Object.class (which is also visible to all instances of Example) or even String.class or some static Object field in Example:
public class Example { private static int n; private static final Object lock = new Object(); ... synchronized(lock) { n++; } ... }
Note that using the current class (Example.class in this case) is just the standard way of doing things.
同步块中对象级别的锁和类级别的锁 —— Thread synchronization, object level locking and class level locking
标签:
原文地址:http://www.cnblogs.com/Guoyutian/p/5078033.html