Java synchronized簡要總結

2021-07-28 03:20:47 字數 4646 閱讀 8032

synchronized修飾的方法,記住:

1.非靜態方法

同步鎖synchronized無論是用於方法(synchronized method)還是類(synchronized class)

(1)如果該類物件是唯一的,比如

//..省略兩個被同步鎖修飾的test1和test2方法和main方法和類名testsyn

final testsyn myt = new testsyn();//唯一的

thread test1 = new thread( new runnable() }, 「test1」 ); //該類物件

thread test2 = new thread( new runnable() }, 「test2」 ); //該類物件

此時執行緒test1執行方法test1時執行緒test2將無法訪問test2方法,原因是將模擬作房屋而裡面的同步鎖即為房間的鎖,乙個類僅有一把鑰匙,此時執行緒test1先拿到鑰匙進房屋開始訪問,而執行緒2沒用只有等執行緒1訪問完才能執行。附**如下:

public class testsyn

catch (interruptedexception ie) }

}

public  synchronized void test2()   

catch (interruptedexception ie)

}

public static void main(string args)   

}, "test1" );

thread test2 = new thread( new runnable() }, "test2" );

test1.start();

test2.start();

}

}

輸出結果如下:

test1 : 4

test1 : 3

test1 : 2

test1 : 1

test1 : 0

test2 : 4

test2 : 3

test2 : 2

test2 : 1

test2 : 0

注意:synchronized鎖類和鎖非靜態方法效果是一樣的,看下面:

public class testsynchronized

catch (interruptedexception ie) }

} }

public synchronized void test2()

catch (interruptedexception ie) }

} public static void main(string args)

}, 「test1」 );

thread test2 = new thread( new runnable() }, 「test2」 );

test1.start();;

test2.start();

} }

輸出結果如下:

test1 : 4

test1 : 3

test1 : 2

test1 : 1

test1 : 0

test2 : 4

test2 : 3

test2 : 2

test2 : 1

test2 : 0

(2)但是如果該類被不同物件引用,如下:

public class testsyn

catch (interruptedexception ie) }

}

public  synchronized void test2()   

catch (interruptedexception ie)

}

public static void main(string args)   

}, "test1" );

thread test2 = new thread( new runnable() }, "test2" );

test1.start();

test2.start();

}

}

輸出結果如下:

test1 : 4

test2 : 4

test1 : 3

test2 : 3

test1 : 2

test2 : 2

test1 : 1

test2 : 1

test1 : 0

test2 : 0

發現執行緒互不干擾,很簡單,理解為兩個房屋兩把鑰匙,那自然不會影響咯.

2.靜態方法

明白靜態是所有類共有的,靜態方法在jvm剛載入的時候就編譯過了,在程式的執行過程中隨時可以呼叫…不需要去例項化某個物件然後再去呼叫,可以直接用類名去呼叫…不過你想一下,在jvm剛載入的進修就編譯過了也就是說它一直存在著…也就是說它一直占用這記憶體中的位址空間,所以說也是比較佔資源的,這和非靜態方法是不一樣的,所以,直接上**看結果:

public class testsyn

catch (interruptedexception ie) }

}

public  synchronized void test2()   

catch (interruptedexception ie)

}

public static void main(string args)   

}, "test1" );

thread test2 = new thread( new runnable() }, "test2" );

test1.start();

test2.start();

}

}

輸出結果:

test1 : 4

test1 : 3

test1 : 2

test1 : 1

test1 : 0

test2 : 4

test2 : 3

test2 : 2

test2 : 1

test2 : 0

上述**引用同乙個物件的同乙個方法(還是靜態方法),會被鎖,很正常,如果是不同物件的靜態方法,應該還是會鎖:

public class testsyn

catch (interruptedexception ie) }

}

public  synchronized void test2()   

catch (interruptedexception ie)

}

public static void main(string args)   

}, "test1" );

thread test2 = new thread( new runnable() }, "test2" );

test1.start();

test2.start();

}

}

輸出結果:

test1 : 4

test1 : 3

test1 : 2

test1 : 1

test1 : 0

test2 : 4

test2 : 3

test2 : 2

test2 : 1

test2 : 0

這很好理解,但是如果引用的不是同乙個物件的話,那肯定不會被鎖,現在看下如果是兩個靜態,顯然肯定被鎖,看下:

public class testsyn

catch (interruptedexception ie) }

}

public static synchronized void test2()   

catch (interruptedexception ie)

}

public static void main(string args)   

}, "test1" );

thread test2 = new thread( new runnable() }, "test2" );

test1.start();

test2.start();

}

}

結果:

test1 : 4

test1 : 3

test1 : 2

test1 : 1

test1 : 0

test2 : 4

test2 : 3

test2 : 2

test2 : 1

test2 : 0

沒錯就是這樣.其他都類似,就不分析了.

java synchronized 簡單示例

public class sequentialexample class box public string setname string name public string getname class sequential extends thread public void run else ...

java synchronized同步使用方法

函式前加synchronized時,用的是this物件,如果要實現 塊中synchronized中 與synchronized函式的 間同步,那 塊的synchronized要使用this物件。synchronized this synchronized 同步原理 當乙個執行緒進入synchroni...

java synchronized的幾個規則

對synchronized this 的一些理解 一 當兩個併發執行緒訪問同乙個物件object中的這個synchronized this 同步 塊時,乙個時間內只能有乙個執行緒得到執行。另乙個執行緒必須等待當前執行緒執行完這個 塊以後才能執行該 塊。二 然而,當乙個執行緒訪問object的乙個sy...