Java多線程訪問Synchronized同步方法的八種使用場景
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號”
優(yōu)質(zhì)文章,第一時間送達(dá)
簡介
八種使用場景:
場景一:兩個線程同時訪問同一個對象的同步方法
兩個線程同時訪問同一個對象的同步方法,是線程安全的。
場景二:兩個線程同時訪問兩個對象的同步方法
兩個線程同時訪問兩個對象的同步方法,是線程不安全的。
代碼驗(yàn)證:
public class Condition2 implements Runnable {
// 創(chuàng)建兩個不同的對象
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("測試結(jié)束");
}
}
運(yùn)行結(jié)果:
線程名:Thread-0,運(yùn)行開始
線程名:Thread-1,運(yùn)行開始
線程:Thread-0,運(yùn)行結(jié)束
線程:Thread-1,運(yùn)行結(jié)束
測試結(jié)束
代碼分析:
場景三:兩個線程同時訪問(一個或兩個)對象的靜態(tài)同步方法
兩個線程同時訪問(一個或兩個)對象的靜態(tài)同步方法,是線程安全的。
場景四:兩個線程分別同時訪問(一個或兩個)對象的同步方法和非同步方法
兩個線程分別同時訪問(一個或兩個)對象的同步方法和非同步方法,是線程不安全的。
public class Condition4 implements Runnable {
static Condition4 instance = new Condition4();
@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();
}
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("測試結(jié)束");
}
}
運(yùn)行結(jié)果:
線程名:Thread-0,同步方法,運(yùn)行開始
線程名:Thread-1,普通方法,運(yùn)行開始
線程:Thread-0,同步方法,運(yùn)行結(jié)束
線程:Thread-1,普通方法,運(yùn)行結(jié)束
測試結(jié)束
結(jié)果分析
非同步方法不受其它由synchronized修飾的同步方法影響
場景五:兩個線程訪問同一個對象中的同步方法,同步方法又調(diào)用一個非同步方法
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);
// 這兩個線程直接調(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("測試結(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é)束
測試結(jié)束
結(jié)果分析:
兩個線程訪問同一個對象中的同步方法,同步方法又調(diào)用一個非同步方法,僅在沒有其他線程直接調(diào)用非同步方法的情況下,是線程安全的。若有其他線程直接調(diào)用非同步方法,則是線程不安全的。
場景六:兩個線程同時訪問同一個對象的不同的同步方法
兩個線程同時訪問同一個對象的不同的同步方法時,是線程安全的。
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("測試結(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é)束
測試結(jié)束
結(jié)果分析:
場景七:兩個線程分別同時訪問靜態(tài)synchronized和非靜態(tài)synchronized方法
兩個線程分別同時訪問靜態(tài)synchronized和非靜態(tài)synchronized方法,線程不安全。
代碼實(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 修飾的方法,屬于類鎖,鎖對象為(*.class)對象。
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 修飾的方法,屬于方法鎖,鎖對象為(this)對象。
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)對象,線程2的鎖是方法鎖(this)對象,兩個線程的鎖不一樣,自然不會互相影響,所以會并行執(zhí)行。
Thread thread1 = new Thread(instance);
Thread thread2 = new Thread(instance);
thread1.start();
thread2.start();
while (thread1.isAlive() || thread2.isAlive()) {
}
System.out.println("測試結(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é)束
測試結(jié)束
場景八:同步方法拋出異常后,JVM會自動釋放鎖的情況
只有當(dāng)同步方法執(zhí)行完或執(zhí)行時拋出異常這兩種情況,才會釋放鎖。
代碼實(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)拋出異常時,JVM會自動釋放鎖,不需要手動釋放,其他線程即可獲取到該鎖
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("測試結(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é)束
測試結(jié)束
結(jié)果分析:
總結(jié)
作者 | 大腦補(bǔ)丁
來源 | https://blog.csdn.net/x541211190/article/details/106272922

評論
圖片
表情
