publicvoidh() {
  // other operations should not be locked...
  System.out.println(Thread.currentThread().getName()
  +":not synchronized in h()");
  lock.lock();
  try{
  for(inti = 0; i < 5; i++) {
  System.out.println(Thread.currentThread().getName()
  +":synchronized in h()");
  try{
  TimeUnit.SECONDS.sleep(3);
  }catch(InterruptedException e) {
  e.printStackTrace();
  }
  }
  }finally{
  lock.unlock();
  }
  }
  publicstaticvoidmain(String[] args) {
  finalResource3 rs =newResource3();
  newThread() {
  publicvoidrun() {
  rs.f();
  }
  }.start();
  newThread() {
  publicvoidrun() {
  rs.g();
  }
  }.start();
  rs.h();
  }
  }
  结果:
  Thread-0:not synchronized in f()
  Thread-0:synchronized in f()
  main:not synchronized in h()
  Thread-1:not synchronized in g()
  Thread-0:synchronized in f()
  Thread-0:synchronized in f()
  Thread-0:synchronized in f()
  Thread-0:synchronized in f()
  main:synchronized in h()
  main:synchronized in h()
  main:synchronized in h()
  main:synchronized in h()
  main:synchronized in h()
  Thread-1:synchronized in g()
  Thread-1:synchronized in g()
  Thread-1:synchronized in g()
  Thread-1:synchronized in g()
  Thread-1:synchronized in g()
  Resource4.java
  packagecom.zj.lock;
  importjava.util.concurrent.TimeUnit;
  importjava.util.concurrent.locks.Lock;
  importjava.util.concurrent.locks.ReentrantLock;
  publicclassResource4 {
  privateLocklock1=newReentrantLock();
  privateLocklock2=newReentrantLock();
  privateLocklock3=newReentrantLock();
  publicvoidf() {
  // other operations should not be locked...
  System.out.println(Thread.currentThread().getName()
  +":not synchronized in f()");
  lock1.lock();
  try{
  for(inti = 0; i < 5; i++) {
  System.out.println(Thread.currentThread().getName()
  +":synchronized in f()");
  try{
  TimeUnit.SECONDS.sleep(3);
  }catch(InterruptedException e) {
  e.printStackTrace();
  }
  }
  }finally{
  lock1.unlock();
  }
  }
  publicvoidg() {
  // other operations should not be locked...
  System.out.println(Thread.currentThread().getName()
  +":not synchronized in g()");
  lock2.lock();
  try{
  for(inti = 0; i < 5; i++) {
  System.out.println(Thread.currentThread().getName()
  +":synchronized in g()");
  try{
  TimeUnit.SECONDS.sleep(3);
  }catch(InterruptedException e) {
  e.printStackTrace();
  }
  }
  }finally{
  lock2.unlock();
  }
  }
  publicvoidh() {
  // other operations should not be locked...
  System.out.println(Thread.currentThread().getName()
  +":not synchronized in h()");
  lock3.lock();
  try{
  for(inti = 0; i < 5; i++) {
  System.out.println(Thread.currentThread().getName()
  +":synchronized in h()");
  try{
  TimeUnit.SECONDS.sleep(3);
  }catch(InterruptedException e) {
  e.printStackTrace();
  }
  }
  }finally{
  lock3.unlock();
  }
  }
  publicstaticvoidmain(String[] args) {
  finalResource4 rs =newResource4();
  newThread() {
  publicvoidrun() {
  rs.f();
  }
  }.start();
  newThread() {
  publicvoidrun() {
  rs.g();
  }
  }.start();
  rs.h();
  }
  }
  结果:
  Thread-0:not synchronized in f()
  Thread-0:synchronized in f()
  main:not synchronized in h()
  main:synchronized in h()
  Thread-1:not synchronized in g()
  Thread-1:synchronized in g()
  Thread-0:synchronized in f()
  main:synchronized in h()
  Thread-1:synchronized in g()
  Thread-0:synchronized in f()
  main:synchronized in h()
  Thread-1:synchronized in g()
  Thread-0:synchronized in f()
  main:synchronized in h()
  Thread-1:synchronized in g()
  Thread-0:synchronized in f()
  main:synchronized in h()
  Thread-1:synchronized in g()
  synchronized和lock的区别:
  Lock 的锁定是通过代码实现的,而 synchronized 是在 JVM 层面上实现的
  synchronized 在锁定时如果方法块抛出异常,JVM 会自动将锁释放掉,不会因为出了异常没有释放锁造成线程死锁。但是 Lock 的话享受不到 JVM 带来自动的功能,出现异常时必须在 finally 将锁释放掉,否则将会引起死锁。
  在资源竞争不是很激烈的情况下,偶尔会有同步的情形下,synchronized是很合适的。原因在于,编译程序通常会尽可能的进行优化synchronize,另外可读性非常好,不管用没用过5.0多线程包的程序员都能理解。
  ReentrantLock:
  ReentrantLock提供了多样化的同步,比如有时间限制的同步,可以被Interrupt的同步(synchronized的同步是不能Interrupt的)等。在资源竞争不激烈的情形下,性能稍微比synchronized差点点。但是当同步非常激烈的时候,synchronized的性能一下子能下降好几十倍。而ReentrantLock确还能维持常态。
  Atomic:
  和上面的类似,不激烈情况下,性能比synchronized略逊,而激烈的时候,也能维持常态。激烈的时候,Atomic的性能会优于ReentrantLock一倍左右。但是其有一个缺点,是只能同步一个值,一段代码中只能出现一个Atomic的变量,多于一个同步无效。因为他不能在多个Atomic之间同步。