Java多線程中的互斥鎖解析
基本介紹
- Java語言中,引入了對象互斥鎖的概念,來保證共享數(shù)據(jù)操作的完整性。
- 每個(gè)對象都對應(yīng)于一個(gè)可稱為 “ 互斥鎖 ” 的標(biāo)記,這個(gè)標(biāo)記用來保證在任一時(shí)刻,只能有一個(gè)線程訪問該對象。
- 關(guān)鍵字 synchronized 來與對象的互斥鎖聯(lián)系。當(dāng)某個(gè)對象用 synchronized 修飾時(shí),表明該對象在任一時(shí)刻只能由一個(gè)線程訪問
- 同步的局限性:導(dǎo)致程序的執(zhí)行效率要降低
- 同步方法(非靜態(tài)的)的鎖可以是this,也可以是其他對象(要求是同一個(gè)對象)
- 同步方法(靜態(tài)的)的鎖為當(dāng)前類本身。
把 synchronized 寫在代碼塊上
package thread_;
/**
* @Author: Gin
* @Description:
* @Modified By: Gin
* @Date: Created in 16:37 2021/9/27
*/
public class Thread11 {
public static void main(String[] args) {
SellThread04 sellThread04 = new SellThread04();
new Thread(sellThread04).start();
new Thread(sellThread04).start();
new Thread(sellThread04).start();
}
}
class SellThread04 implements Runnable{
private int ticketsNum = 100;
private boolean flag = true;
@Override
public void run() {
while (flag) {
sell();
}
}
/*
1. public synchronized void sell(){} 就是一個(gè)同步方法
2. 這時(shí)“鎖”在 this 對象上
3. 也可以在代碼塊上寫 synchronized,同步代碼塊,互斥鎖還是在 this 對象上
如下:
*/
public /* synchronized */ void sell(){
synchronized (this) {
if(ticketsNum <= 0){
System.out.println("售票結(jié)束...");
flag = false;
return;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("窗口 " + Thread.currentThread().getName() + " 售票一張, 剩余票數(shù) " + (--ticketsNum));
}
}
}測試操作同一個(gè)對象、靜態(tài)方法的鎖的添加
package thread_;
/**
* @Author: Gin
* @Description:
* @Modified By: Gin
* @Date: Created in 16:37 2021/9/27
*/
public class Thread11 {
public static void main(String[] args) {
SellThread04 sellThread04 = new SellThread04();
new Thread(sellThread04).start();
new Thread(sellThread04).start();
new Thread(sellThread04).start();
}
}
class SellThread04 implements Runnable{
private int ticketsNum = 100;
private boolean flag = true;
// 同步方法(非靜態(tài)的)的鎖可以是this,也可以是其他對象(要求是同一個(gè)對象)
// new 一個(gè) Object 對象,測試操作同一個(gè)對象
Object object = new Object();
// 同步方法(靜態(tài)的)的鎖為當(dāng)前類本身。
public synchronized static void m1(){
System.out.println("m1");
}
// 在靜態(tài)方法中實(shí)現(xiàn)一個(gè)同步代碼塊
public static void m2(){
synchronized (SellThread04.class) {
System.out.println("m2");
}
}
@Override
public void run() {
while (flag) {
sell();
}
}
/*
1. public synchronized void sell(){} 就是一個(gè)同步方法
2. 這時(shí)“鎖”在 this 對象上
3. 也可以在代碼塊上寫 synchronized,同步代碼塊,互斥鎖還是在 this 對象上
如下:
*/
public /* synchronized */ void sell(){
synchronized ( /*this*/ object ) { // 測試操作同一個(gè)對象
if(ticketsNum <= 0){
System.out.println("售票結(jié)束...");
flag = false;
return;
}
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("窗口 " + Thread.currentThread().getName() + " 售票一張, 剩余票數(shù) " + (--ticketsNum));
}
}
}細(xì)節(jié)
同步方法如果沒有使用 static 修飾:默認(rèn)鎖對象為 this如果方法使用 static 修飾,默認(rèn)鎖對象:當(dāng)前類.class
到此這篇關(guān)于Java多線程中的互斥鎖解析的文章就介紹到這了,更多相關(guān)Java互斥鎖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MyBatis-Plus中最簡單的查詢操作教程(Lambda)
這篇文章主要給大家介紹了關(guān)于MyBatis-Plus中最簡單的查詢操作的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-03-03
Java將RTF轉(zhuǎn)換為PDF格式的實(shí)現(xiàn)
本文主要介紹了Java將RTF轉(zhuǎn)換為PDF格式的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
使用Spring Boot輕松實(shí)現(xiàn)流式AI輸出的步驟
本文介紹了如何使用Spring Boot和WebFlux實(shí)現(xiàn)流式AI輸出,通過非阻塞I/O、反應(yīng)式編程和函數(shù)式路由等技術(shù),優(yōu)化了AI應(yīng)用的響應(yīng)速度,提升了用戶體驗(yàn),感興趣的朋友一起看看吧2025-02-02
java線程之使用Runnable接口創(chuàng)建線程的方法
本篇文章介紹了,java中使用Runnable接口創(chuàng)建線程的方法。需要的朋友參考下2013-05-05
kotlin java 混合代碼 maven 打包實(shí)現(xiàn)
這篇文章主要介紹了kotlin java 混合代碼 maven 打包實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03
詳解Java如何在Array和List之間進(jìn)行轉(zhuǎn)換
這篇文章主要為大家介紹了詳解Java如何在Array和List之間進(jìn)行轉(zhuǎn)換的方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
解決IDEA?JDK9沒有module-info.java的問題
這篇文章主要介紹了解決IDEA?JDK9沒有module-info.java的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01

