synchronized是java中用於同步的關鍵字,一般我們通過Synchronized鎖住一個對象,來進行線程同步。我們需要了解在程序執行過程中,synchronized鎖住的到底是哪個對象,否則我們在多線程的程序就有可能出現問題。
看下面的代碼,我們定義了一個靜態變量n,在run方法中,我們使n增加10,然後在main方法中,我們開闢了100個線程,來執行n增加的操作,如果線程沒有併發執行,那麼n最後的值應該爲1000,顯然下面的程序執行完結果不是1000,因爲我們沒有進行線程同步。
import java.util.concurrent.TimeUnit;
public class SynchronizedTest1 extends Thread {
public static int n = 0;
public void run() {
try {
//使n自加10次
for (int i = 0; i < 10; i++) {
n = n + 1;
TimeUnit.MILLISECONDS.sleep(10);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[100];
for (int i = 0; i < threads.length; i++) {
threads[i] = new SynchronizedTest1();
threads[i].start();
}
//使所有其他線程執行完,再繼續執行main線程,這樣得出的n是最終的結果
for (Thread thread : threads) {
thread.join();
}
System.out.println(n);
}
}
爲了實現同步,我們修改上面的代碼,增加一個increase方法,如下。但是當我們執行下面的代碼時,會發現n仍然不是1000.
import java.util.concurrent.TimeUnit;
public class SynchronizedTest2 extends Thread {
public static int n = 0;
public synchronized void increase() {
n++;
}
public void run() {
try {
//使n自加10次
for (int i = 0; i < 10; i++) {
increase();
TimeUnit.MILLISECONDS.sleep(10);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[100];
for (int i = 0; i < threads.length; i++) {
threads[i] = new SynchronizedTest2();
threads[i].start();
}
//使所有其他線程執行完,再繼續執行main線程,這樣得出的n是最終的結果
for (Thread thread : threads) {
thread.join();
}
System.out.println(n);
}
}
其實原因很簡單,上面的多個線程在執行時根本就沒有競爭同一個對象鎖。當我們執行用synchronized修飾的非靜態方法時,線程會首先獲得調用這個方法的對象的鎖,然後才能繼續執行代碼。那麼調用這個方法的到底是哪個對象,是this對象。在上面的例子中,thread[i]所代表的線程獲取的鎖對象是thread[i]對象,也就是該線程對象本身。因此上面所開闢的100個線程只要獲得自身對象就可以執行,這樣就使同步失去了作用。
我們再次修改代碼:即將increase方法改爲i靜態的,此時程序執行完後n的值爲1000。
import java.util.concurrent.TimeUnit;
public class SynchronizedTest3 extends Thread {
public static int n = 0;
public synchronized static void increase() {
n++;
}
public void run() {
try {
//使n自加10次
for (int i = 0; i < 10; i++) {
increase();
TimeUnit.MILLISECONDS.sleep(10);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[100];
for (int i = 0; i < threads.length; i++) {
threads[i] = new SynchronizedTest3();
threads[i].start();
}
//使所有其他線程執行完,再繼續執行main線程,這樣得出的n是最終的結果
for (Thread thread : threads) {
thread.join();
}
System.out.println(n);
}
}
當synchronized 修飾static方法,它鎖住的是該類的Class對象,而不是某一個具體對象。在上面的例子中,它鎖住的就是SynchronizedTest3.class對象。在程序執行過程中,類的Class對象只有一份,所以上面線程競爭的是同一個對象鎖。
下面是對synchronized鎖住對象的總結:
(1)對於同步方法,鎖當前對象(this)
(2)對於靜態同步方法,鎖當前類的Class對象
(3)對於同步代碼塊,鎖住的是synchronized括號中的對象