Dowemo

Why do you want to use synchronization in the . * * * * * * * * * * * * * * * * * *
Java allows multithreading concurrency control, when multiple threads operate on a shared resource variable ( such as adding and removing data from the data ).
It'll result in inaccurate data and conflicts with each other, so join a synchronous lock to avoid calls to other threads before the thread doesn't complete
So the uniqueness and accuracy of this variable are guaranteed.

1. * * * * * * * * * * *.
That's, the method of ( synchronous ) modification.
Because each object of java has a built-in lock, it's modified with this keyword.
Built-in locks protect the entire method. A built-in lock is required before the method is called, otherwise it's blocked.



代码如:


public synchronized void save(){}





Note: the synchronized keyword can also modify static methods, which will lock the entire class if it's called

* * * * * * * * * * *
A statement block with a synchronized keyword modifier.

 代码如:


 synchronized(object){


 }


3. * * * * * * * * * * *.



在JavaSE5.0中新增了一个java.util.concurrent包来支持同步。


ReentrantLock类是可重入、互斥、实现了Lock接口的锁,


它与使用synchronized方法和快具有相同的基本行为和语义,并且扩展了其能力



ReenreantLock类的常用方法有:



 ReentrantLock() : 创建一个ReentrantLock实例


 lock() : 获得锁


 unlock() : 释放锁


注:ReentrantLock()还有一个可以创建公平锁的构造方法,但由于能大幅度降低程序运行效率,不推荐使用 





,.
这里写图片描述




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs