【186期】一口氣說出 Synchronized 同步方法的八種使用場(chǎng)景
閱讀本文大概需要 12 分鐘。
來自:blog.csdn.net/x541211190/article/details/106272922
簡(jiǎn)介
八種使用場(chǎng)景:
兩個(gè)線程同時(shí)訪問同一個(gè)對(duì)象的同步方法
兩個(gè)線程同時(shí)訪問兩個(gè)對(duì)象的同步方法
兩個(gè)線程同時(shí)訪問(一個(gè)或兩個(gè))對(duì)象的靜態(tài)同步方法
兩個(gè)線程分別同時(shí)訪問(一個(gè)或兩個(gè))對(duì)象的同步方法和非同步方法
兩個(gè)線程訪問同一個(gè)對(duì)象中的同步方法,同步方法又調(diào)用一個(gè)非同步方法
兩個(gè)線程同時(shí)訪問同一個(gè)對(duì)象的不同的同步方法
兩個(gè)線程分別同時(shí)訪問靜態(tài)synchronized和非靜態(tài)synchronized方法
同步方法拋出異常后,JVM會(huì)自動(dòng)釋放鎖的情況
場(chǎng)景一:兩個(gè)線程同時(shí)訪問同一個(gè)對(duì)象的同步方法
場(chǎng)景二:兩個(gè)線程同時(shí)訪問兩個(gè)對(duì)象的同步方法
代碼驗(yàn)證:
public class Condition2 implements Runnable {
// 創(chuàng)建兩個(gè)不同的對(duì)象
static Condition2 instance1 = new Condition2();
static Condition2 instance2 = new Condition2();
@Override
public void run() {
method();
}
private synchronized void method() {
System.out.println("線程名:" + Thread.currentThread().getName() + ",運(yùn)行開始");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程:" + Thread.currentThread().getName() + ",運(yùn)行結(jié)束");
}
public static void main(String[] args) {
Thread thread1 = new Thread(instance1);
Thread thread2 = new Thread(instance2);
thread1.start();
thread2.start();
while (thread1.isAlive() || thread2.isAlive()) {
}
System.out.println("測(cè)試結(jié)束");
}
}
運(yùn)行結(jié)果:
線程名:Thread-0,運(yùn)行開始
線程名:Thread-1,運(yùn)行開始
線程:Thread-0,運(yùn)行結(jié)束
線程:Thread-1,運(yùn)行結(jié)束
測(cè)試結(jié)束
代碼分析:
場(chǎng)景三:兩個(gè)線程同時(shí)訪問(一個(gè)或兩個(gè))對(duì)象的靜態(tài)同步方法
場(chǎng)景四:兩個(gè)線程分別同時(shí)訪問(一個(gè)或兩個(gè))對(duì)象的同步方法和非同步方法
我們可以確定是線程不安全的,如果方法不加
synchronized都是安全的,那就不需要同步方法了。驗(yàn)證下我們的結(jié)論:public class Condition4 implements Runnable {
static Condition4 instance = new Condition4();
@Override
public void run() {
//兩個(gè)線程訪問同步方法和非同步方法
if (Thread.currentThread().getName().equals("Thread-0")) {
//線程0,執(zhí)行同步方法method0()
method0();
}
if (Thread.currentThread().getName().equals("Thread-1")) {
//線程1,執(zhí)行非同步方法method1()
method1();
}
}
// 同步方法
private synchronized void method0() {
System.out.println("線程名:" + Thread.currentThread().getName() + ",同步方法,運(yùn)行開始");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程:" + Thread.currentThread().getName() + ",同步方法,運(yùn)行結(jié)束");
}
// 普通方法
private void method1() {
System.out.println("線程名:" + Thread.currentThread().getName() + ",普通方法,運(yùn)行開始");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程:" + Thread.currentThread().getName() + ",普通方法,運(yùn)行結(jié)束");
}
public static void main(String[] args) {
Thread thread1 = new Thread(instance);
Thread thread2 = new Thread(instance);
thread1.start();
thread2.start();
while (thread1.isAlive() || thread2.isAlive()) {
}
System.out.println("測(cè)試結(jié)束");
}
}
運(yùn)行結(jié)果:
線程名:Thread-0,同步方法,運(yùn)行開始
線程名:Thread-1,普通方法,運(yùn)行開始
線程:Thread-0,同步方法,運(yùn)行結(jié)束
線程:Thread-1,普通方法,運(yùn)行結(jié)束
測(cè)試結(jié)束
結(jié)果分析
場(chǎng)景五:兩個(gè)線程訪問同一個(gè)對(duì)象中的同步方法,同步方法又調(diào)用一個(gè)非同步方法
public class Condition8 implements Runnable {
static Condition8 instance = new Condition8();
@Override
public void run() {
if (Thread.currentThread().getName().equals("Thread-0")) {
//直接調(diào)用普通方法
method2();
} else {
// 先調(diào)用同步方法,在同步方法內(nèi)調(diào)用普通方法
method1();
}
}
// 同步方法
private static synchronized void method1() {
System.out.println("線程名:" + Thread.currentThread().getName() + ",同步方法,運(yùn)行開始");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程:" + Thread.currentThread().getName() + ",同步方法,運(yùn)行結(jié)束,開始調(diào)用普通方法");
method2();
}
// 普通方法
private static void method2() {
System.out.println("線程名:" + Thread.currentThread().getName() + ",普通方法,運(yùn)行開始");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程:" + Thread.currentThread().getName() + ",普通方法,運(yùn)行結(jié)束");
}
public static void main(String[] args) {
// 此線程直接調(diào)用普通方法
Thread thread0 = new Thread(instance);
// 這兩個(gè)線程直接調(diào)用同步方法
Thread thread1 = new Thread(instance);
Thread thread2 = new Thread(instance);
thread0.start();
thread1.start();
thread2.start();
while (thread0.isAlive() || thread1.isAlive() || thread2.isAlive()) {
}
System.out.println("測(cè)試結(jié)束");
}
}
運(yùn)行結(jié)果:
線程名:Thread-0,普通方法,運(yùn)行開始
線程名:Thread-1,同步方法,運(yùn)行開始
線程:Thread-1,同步方法,運(yùn)行結(jié)束,開始調(diào)用普通方法
線程名:Thread-1,普通方法,運(yùn)行開始
線程:Thread-0,普通方法,運(yùn)行結(jié)束
線程:Thread-1,普通方法,運(yùn)行結(jié)束
線程名:Thread-2,同步方法,運(yùn)行開始
線程:Thread-2,同步方法,運(yùn)行結(jié)束,開始調(diào)用普通方法
線程名:Thread-2,普通方法,運(yùn)行開始
線程:Thread-2,普通方法,運(yùn)行結(jié)束
測(cè)試結(jié)束
結(jié)果分析:
synchronized關(guān)鍵字,使其變成一個(gè)同步方法,這樣就變成了《場(chǎng)景五:兩個(gè)線程同時(shí)訪問同一個(gè)對(duì)象的不同的同步方法》,這種場(chǎng)景下,大家就很清楚的看到,同一個(gè)對(duì)象中的兩個(gè)同步方法,不管哪個(gè)線程調(diào)用,都是線程安全的了。場(chǎng)景六:兩個(gè)線程同時(shí)訪問同一個(gè)對(duì)象的不同的同步方法
public class Condition5 implements Runnable {
static Condition5 instance = new Condition5();
@Override
public void run() {
if (Thread.currentThread().getName().equals("Thread-0")) {
//線程0,執(zhí)行同步方法method0()
method0();
}
if (Thread.currentThread().getName().equals("Thread-1")) {
//線程1,執(zhí)行同步方法method1()
method1();
}
}
private synchronized void method0() {
System.out.println("線程名:" + Thread.currentThread().getName() + ",同步方法0,運(yùn)行開始");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程:" + Thread.currentThread().getName() + ",同步方法0,運(yùn)行結(jié)束");
}
private synchronized void method1() {
System.out.println("線程名:" + Thread.currentThread().getName() + ",同步方法1,運(yùn)行開始");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程:" + Thread.currentThread().getName() + ",同步方法1,運(yùn)行結(jié)束");
}
//運(yùn)行結(jié)果:串行
public static void main(String[] args) {
Thread thread1 = new Thread(instance);
Thread thread2 = new Thread(instance);
thread1.start();
thread2.start();
while (thread1.isAlive() || thread2.isAlive()) {
}
System.out.println("測(cè)試結(jié)束");
}
}
運(yùn)行結(jié)果:
線程名:Thread-1,同步方法1,運(yùn)行開始
線程:Thread-1,同步方法1,運(yùn)行結(jié)束
線程名:Thread-0,同步方法0,運(yùn)行開始
線程:Thread-0,同步方法0,運(yùn)行結(jié)束
測(cè)試結(jié)束
結(jié)果分析:
所以對(duì)于同一個(gè)實(shí)例(instance),兩個(gè)線程拿到的鎖是同一把鎖,此時(shí)同步方法會(huì)串行執(zhí)行。這也是
synchronized關(guān)鍵字的可重入性的一種體現(xiàn)。場(chǎng)景七:兩個(gè)線程分別同時(shí)訪問靜態(tài)synchronized和非靜態(tài)synchronized方法
synchronized方法屬于類鎖,鎖對(duì)象是(*.class)對(duì)象,非靜態(tài)synchronized方法屬于對(duì)象鎖中的方法鎖,鎖對(duì)象是this對(duì)象。兩個(gè)線程拿到的是不同的鎖,自然不會(huì)相互影響。結(jié)論:代碼實(shí)現(xiàn):
public class Condition6 implements Runnable {
static Condition6 instance = new Condition6();
@Override
public void run() {
if (Thread.currentThread().getName().equals("Thread-0")) {
//線程0,執(zhí)行靜態(tài)同步方法method0()
method0();
}
if (Thread.currentThread().getName().equals("Thread-1")) {
//線程1,執(zhí)行非靜態(tài)同步方法method1()
method1();
}
}
// 重點(diǎn):用static synchronized 修飾的方法,屬于類鎖,鎖對(duì)象為(*.class)對(duì)象。
private static synchronized void method0() {
System.out.println("線程名:" + Thread.currentThread().getName() + ",靜態(tài)同步方法0,運(yùn)行開始");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程:" + Thread.currentThread().getName() + ",靜態(tài)同步方法0,運(yùn)行結(jié)束");
}
// 重點(diǎn):synchronized 修飾的方法,屬于方法鎖,鎖對(duì)象為(this)對(duì)象。
private synchronized void method1() {
System.out.println("線程名:" + Thread.currentThread().getName() + ",非靜態(tài)同步方法1,運(yùn)行開始");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程:" + Thread.currentThread().getName() + ",非靜態(tài)同步方法1,運(yùn)行結(jié)束");
}
//運(yùn)行結(jié)果:并行
public static void main(String[] args) {
//問題原因: 線程1的鎖是類鎖(*.class)對(duì)象,線程2的鎖是方法鎖(this)對(duì)象,兩個(gè)線程的鎖不一樣,自然不會(huì)互相影響,所以會(huì)并行執(zhí)行。
Thread thread1 = new Thread(instance);
Thread thread2 = new Thread(instance);
thread1.start();
thread2.start();
while (thread1.isAlive() || thread2.isAlive()) {
}
System.out.println("測(cè)試結(jié)束");
}
運(yùn)行結(jié)果:
線程名:Thread-0,靜態(tài)同步方法0,運(yùn)行開始
線程名:Thread-1,非靜態(tài)同步方法1,運(yùn)行開始
線程:Thread-1,非靜態(tài)同步方法1,運(yùn)行結(jié)束
線程:Thread-0,靜態(tài)同步方法0,運(yùn)行結(jié)束
測(cè)試結(jié)束
場(chǎng)景八:同步方法拋出異常后,JVM會(huì)自動(dòng)釋放鎖的情況
synchronized釋放鎖的場(chǎng)景:代碼實(shí)現(xiàn):
public class Condition7 implements Runnable {
private static Condition7 instance = new Condition7();
@Override
public void run() {
if (Thread.currentThread().getName().equals("Thread-0")) {
//線程0,執(zhí)行拋異常方法method0()
method0();
}
if (Thread.currentThread().getName().equals("Thread-1")) {
//線程1,執(zhí)行正常方法method1()
method1();
}
}
private synchronized void method0() {
System.out.println("線程名:" + Thread.currentThread().getName() + ",運(yùn)行開始");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//同步方法中,當(dāng)拋出異常時(shí),JVM會(huì)自動(dòng)釋放鎖,不需要手動(dòng)釋放,其他線程即可獲取到該鎖
System.out.println("線程名:" + Thread.currentThread().getName() + ",拋出異常,釋放鎖");
throw new RuntimeException();
}
private synchronized void method1() {
System.out.println("線程名:" + Thread.currentThread().getName() + ",運(yùn)行開始");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("線程:" + Thread.currentThread().getName() + ",運(yùn)行結(jié)束");
}
public static void main(String[] args) {
Thread thread1 = new Thread(instance);
Thread thread2 = new Thread(instance);
thread1.start();
thread2.start();
while (thread1.isAlive() || thread2.isAlive()) {
}
System.out.println("測(cè)試結(jié)束");
}
}
運(yùn)行結(jié)果:
線程名:Thread-0,運(yùn)行開始
線程名:Thread-0,拋出異常,釋放鎖
線程名:Thread-1,運(yùn)行開始
Exception in thread "Thread-0" java.lang.RuntimeException
at com.study.synchronize.conditions.Condition7.method0(Condition7.java:34)
at com.study.synchronize.conditions.Condition7.run(Condition7.java:17)
at java.lang.Thread.run(Thread.java:748)
線程:Thread-1,運(yùn)行結(jié)束
測(cè)試結(jié)束
結(jié)果分析:
總結(jié)
synchronized各種使用場(chǎng)景,以及各種場(chǎng)景發(fā)生的原因和結(jié)論。我們分析的理論基礎(chǔ)都是synchronized關(guān)鍵字的鎖對(duì)象究竟是誰?多個(gè)線程之間競(jìng)爭(zhēng)的是否是同一把鎖?根據(jù)這個(gè)條件來判斷線程是否是安全的。所以,有了這些場(chǎng)景的分析鍛煉后,我們?cè)谝院笫褂枚嗑€程編程時(shí),也可以通過分析鎖對(duì)象的方式,判斷出線程是否是安全的,從而避免此類問題的出現(xiàn)。synchronized關(guān)鍵字的最重要的各種使用場(chǎng)景,也是面試官常常會(huì)問到的高頻問題,是一篇值得大家仔細(xì)閱讀和親自動(dòng)手實(shí)踐的文章,喜歡本文請(qǐng)點(diǎn)贊和收藏。推薦閱讀:
【185期】面試官:你能說說 Synchronized實(shí)現(xiàn)對(duì)象鎖的兩種方式以及它的原理嗎?
【184期】SQL數(shù)據(jù)庫(kù)面試題以及答案(50例題)
【182期】SpringCloud常見面試題(2020最新版)
微信掃描二維碼,關(guān)注我的公眾號(hào)
朕已閱 

