日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

synchronized锁的基本用法

發布時間:2025/3/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 synchronized锁的基本用法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在多線程的情況下 需要是同一個對象鎖
Synchronized(對象鎖)
{
需要保證線程安全的代碼
}

1.修飾代碼塊,指定加鎖對象,對給定對象加鎖,進入同步代碼快前要獲得 給定對象 的鎖。
2.修飾實例方法,作用于當前實例加鎖,進入同步代碼前要獲得 當前實例 的鎖
3.修飾靜態方法,作用于當前類對象(當前類.class)加鎖,進入同步代碼前要獲得 當前類對象 的鎖
修飾代碼塊
修飾代碼塊,指定加鎖對象,對給定對象加鎖,進入同步代碼庫前要獲得 給定對象 的鎖。
public class ThreadCount implements Runnable {
private static Integer count = 100;
private String lock = “lock”;

@Override public void run() {while (count > 1) {cal();} }private void cal() {synchronized (this) {try {Thread.sleep(10);} catch (Exception e) {}count--;System.out.println(Thread.currentThread().getName() + "," + count);}}public static void main(String[] args) {ThreadCount threadCount = new ThreadCount();Thread thread1 = new Thread(threadCount);Thread thread2 = new Thread(threadCount);thread1.start();thread2.start(); }

}

修飾實例方法
修飾實例方法,作用于當前實例加鎖,進入同步代碼前要獲得 當前實例的鎖
在實例方法上默認加上synchronized 默認使用this鎖。
public class ThreadCount implements Runnable {
private static Integer count = 100;
private String lock = “lock”;

@Override public void run() {while (count > 1) {cal();} }private synchronized void cal() {try {Thread.sleep(10);} catch (Exception e) {}count--;System.out.println(Thread.currentThread().getName() + "," + count); } public static void main(String[] args) {ThreadCount threadCount = new ThreadCount();Thread thread1 = new Thread(threadCount);Thread thread2 = new Thread(threadCount);thread1.start();thread2.start(); }

}

修飾靜態方法
修飾靜態方法,作用于當前類對象加鎖,進入同步代碼前要獲得 當前類對象的鎖
默認使用當前類的類名.class 鎖

public class ThreadCount implements Runnable {
private static Integer count = 100;
private static String lock = “lock”;

@Override public void run() {while (count > 1) {cal();} }private static void cal() {synchronized (ThreadCount.class) {try {Thread.sleep(10);} catch (Exception e) {}count--;System.out.println(Thread.currentThread().getName() + "," + count);}}public static void main(String[] args) {ThreadCount threadCount1 = new ThreadCount();ThreadCount threadCount2 = new ThreadCount();Thread thread1 = new Thread(threadCount1);Thread thread2 = new Thread(threadCount2);thread1.start();thread2.start(); }

}

synchronized死鎖問題
我們如果在使用synchronized 需要注意 synchronized鎖嵌套的問題 避免死鎖的問題發生。
案例:
public class DeadlockThread implements Runnable {
private int count = 1;
private String lock = “lock”;

@Override public void run() {while (true) {count++;if (count % 2 == 0) {// 線程1需要獲取 lock 在獲取 a方法this鎖// 線程2需要獲取this 鎖在 獲取B方法lock鎖synchronized (lock) {a();}} else {synchronized (this) {b();}}} }public synchronized void a() {System.out.println(Thread.currentThread().getName() + ",a方法..."); }public void b() {synchronized (lock) {System.out.println(Thread.currentThread().getName() + ",b方法...");} }public static void main(String[] args) {DeadlockThread deadlockThread = new DeadlockThread();Thread thread1 = new Thread(deadlockThread);Thread thread2 = new Thread(deadlockThread);thread1.start();thread2.start(); }

}

synchronized 死鎖診斷工具
D:\path\jdk\jdk8\bin\jconsole.exe

線程1 先獲取到自定義對象的lock鎖,進入到a方法需要獲取this鎖
線程2 先獲取this鎖, 進入到b方法需要自定義對象的lock鎖

線程1 線程2 是在同時執行
線程1 線程2
先獲取到自定義對象的lock鎖 先獲取this鎖
需要線程2已經持有的this鎖 線程1已經持有自定義對象的lock鎖

總結

以上是生活随笔為你收集整理的synchronized锁的基本用法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。