Java:使用synchronized和Lock對象獲取對象鎖

在併發環境下,解決共享資源衝突問題時,能夠考慮使用鎖機制。
1. 對象的鎖
全部對象都自動含有單一的鎖。
JVM 負責跟蹤對象被加鎖的次數。若是一個對象被解鎖,其計數變爲 0 。在任務(線程)第一次給對象加鎖的時候,計數變爲 1 。每當這個相同的任務(線程)在此對象上得到鎖時,計數會遞增。
只有首先得到鎖的任務(線程)才能繼續獲取該對象上的多個鎖。
每當任務離開一個 synchronized 方法,計數遞減,當計數爲 0 的時候,鎖被徹底釋放,此時別的任務就能夠使用此資源。
2.synchronized 同步塊
2.1 同步到單一對象鎖
當使用同步塊時,若是方法下的同步塊都同步到一個對象上的鎖,則全部的任務(線程)只能互斥的進入這些同步塊。
Resource1.java 演示了三個線程(包括 main 線程)試圖進入某個類的三個不一樣的方法的同步塊中,雖然這些同步塊處在不一樣的方法中,但因爲是同步到同一個對象(當前對象 synchronized ( this ) ),因此對它們的方法依然是互斥的。
Resource1.java
package com.zj.lock;
import java.util.concurrent.TimeUnit;
 
public class Resource1 {
    public void f() {
       // other operations should not be locked...
       System. out .println(Thread.currentThread().getName()
              + ":not synchronized in f()" );
       synchronized ( this ) {
           for ( int i = 0; i < 5; i++) {
              System. out .println(Thread.currentThread().getName()
                     + ":synchronized in f()" );
              try {
                  TimeUnit. SECONDS .sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       }
    }
 
    public void g() {
       // other operations should not be locked...
       System. out .println(Thread.currentThread().getName()
              + ":not synchronized in g()" );
       synchronized ( this ) {
           for ( int i = 0; i < 5; i++) {
              System. out .println(Thread.currentThread().getName()
                     + ":synchronized in g()" );
              try {
                  TimeUnit. SECONDS .sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       }
    }
 
    public void h() {
       // other operations should not be locked...
       System. out .println(Thread.currentThread().getName()
              + ":not synchronized in h()" );
       synchronized ( this ) {
           for ( int i = 0; i < 5; i++) {
              System. out .println(Thread.currentThread().getName()
                     + ":synchronized in h()" );
              try {
                  TimeUnit. SECONDS .sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       }
    }
 
    public static void main(String[] args) {
       final Resource1 rs = new Resource1();
 
       new Thread() {
           public void run() {
              rs.f();
           }
       }.start();
 
       new Thread() {
           public void run() {
              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()
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()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
main:synchronized in h()
2.2 同步到多個對象鎖
Resource1.java 演示了三個線程(包括 main 線程)試圖進入某個類的三個不一樣的方法的同步塊中,這些同步塊處在不一樣的方法中,而且是同步到三個不一樣的對象( synchronized ( this ) synchronized ( syncObject1 ) synchronized ( syncObject2 ) ),因此對它們的方法中的臨界資源訪問是獨立的。
Resource2.java
package com.zj.lock;
import java.util.concurrent.TimeUnit;
 
public class Resource2 {
    private Object syncObject1 = new Object();
    private Object syncObject2 = new Object();
 
    public void f() {
       // other operations should not be locked...
       System. out .println(Thread.currentThread().getName()
              + ":not synchronized in f()" );
       synchronized ( this ) {
           for ( int i = 0; i < 5; i++) {
              System. out .println(Thread.currentThread().getName()
                     + ":synchronized in f()" );
              try {
                  TimeUnit. SECONDS .sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       }
    }
 
    public void g() {
       // other operations should not be locked...
       System. out .println(Thread.currentThread().getName()
              + ":not synchronized in g()" );
       synchronized ( syncObject1 ) {
           for ( int i = 0; i < 5; i++) {
              System. out .println(Thread.currentThread().getName()
                     + ":synchronized in g()" );
              try {
                  TimeUnit. SECONDS .sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       }
    }
 
    public void h() {
       // other operations should not be locked...
       System. out .println(Thread.currentThread().getName()
              + ":not synchronized in h()" );
       synchronized ( syncObject2 ) {
           for ( int i = 0; i < 5; i++) {
              System. out .println(Thread.currentThread().getName()
                     + ":synchronized in h()" );
              try {
                  TimeUnit. SECONDS .sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       }
    }
 
    public static void main(String[] args) {
       final Resource2 rs = new Resource2();
 
       new Thread() {
           public void run() {
              rs.f();
           }
       }.start();
 
       new Thread() {
           public void run() {
              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()
3.Lock 對象鎖
除了使用 synchronized 外,還能夠使用 Lock 對象來建立臨界區。 Resource3.java 的演示效果同 Resource1.java Resource4.java 的演示效果同 Resource2.java
Resource3.java
package com.zj.lock;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
public class Resource3 {
    private Lock lock = new ReentrantLock();
 
    public void f() {
       // other operations should not be locked...
       System. out .println(Thread.currentThread().getName()
              + ":not synchronized in f()" );
       lock .lock();
       try {
           for ( int i = 0; i < 5; i++) {
              System. out .println(Thread.currentThread().getName()
                     + ":synchronized in f()" );
              try {
                  TimeUnit. SECONDS .sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       } finally {
           lock .unlock();
       }
    }
 
    public void g() {
       // other operations should not be locked...
       System. out .println(Thread.currentThread().getName()
              + ":not synchronized in g()" );
       lock .lock();
       try {
           for ( int i = 0; i < 5; i++) {
              System. out .println(Thread.currentThread().getName()
                     + ":synchronized in g()" );
              try {
                  TimeUnit. SECONDS .sleep(3);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
           }
       } finally {
           lock .unlock();
       }
    }
 
    public void h() {
       // other operations should not be locked...
       System. out .println(Thread.currentThread().getName()
              + ":not synchronized in h()" );
       lock .lock();
       try {
           for ( int i = 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();
       }
    }
 
    public static void main(String[] args) {
       final Resource3 rs = new Resource3();
 
       new Thread() {
           public void run() {
              rs.f();
           }
       }.start();
 
       new Thread() {
           public void run() {
              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
package com.zj.lock;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
public class Resource4 {
    private Lock lock1 = new ReentrantLock();
    private Lock lock2 = new ReentrantLock();
    private Lock lock3 = new ReentrantLock();
 
    public void f() {
       // other operations should not be locked...
       System. out .println(Thread.currentThread().getName()
              + ":not synchronized in f()" );
       lock1 .lock();
       try {
           for ( int i = 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();
       }
    }
 
    public void g() {
       // other operations should not be locked...
       System. out .println(Thread.currentThread().getName()
              + ":not synchronized in g()" );
       lock2 .lock();
       try {
           for ( int i = 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();
       }
    }
 
    public void h() {
       // other operations should not be locked...
       System. out .println(Thread.currentThread().getName()
              + ":not synchronized in h()" );
       lock3 .lock();
       try {
           for ( int i = 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();
       }
    }
 
    public static void main(String[] args) {
       final Resource4 rs = new Resource4();
 
       new Thread() {
           public void run() {
              rs.f();
           }
       }.start();
 
       new Thread() {
           public void run() {
              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()
相關文章
相關標籤/搜索