生活随笔
收集整理的這篇文章主要介紹了
synchronized锁的基本用法
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在多線程的情況下 需要是同一個(gè)對(duì)象鎖
Synchronized(對(duì)象鎖)
{
需要保證線程安全的代碼
}
1.修飾代碼塊,指定加鎖對(duì)象,對(duì)給定對(duì)象加鎖,進(jìn)入同步代碼快前要獲得 給定對(duì)象 的鎖。
2.修飾實(shí)例方法,作用于當(dāng)前實(shí)例加鎖,進(jìn)入同步代碼前要獲得 當(dāng)前實(shí)例 的鎖
3.修飾靜態(tài)方法,作用于當(dāng)前類對(duì)象(當(dāng)前類.class)加鎖,進(jìn)入同步代碼前要獲得 當(dāng)前類對(duì)象 的鎖
修飾代碼塊
修飾代碼塊,指定加鎖對(duì)象,對(duì)給定對(duì)象加鎖,進(jìn)入同步代碼庫(kù)前要獲得 給定對(duì)象 的鎖。
public class ThreadCount implements Runnable {
private static Integer count = 100;
private String lock = “l(fā)ock”;
@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();
}
}
修飾實(shí)例方法
修飾實(shí)例方法,作用于當(dāng)前實(shí)例加鎖,進(jìn)入同步代碼前要獲得 當(dāng)前實(shí)例的鎖
在實(shí)例方法上默認(rèn)加上synchronized 默認(rèn)使用this鎖。
public class ThreadCount implements Runnable {
private static Integer count = 100;
private String lock = “l(fā)ock”;
@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();
}
}
修飾靜態(tài)方法
修飾靜態(tài)方法,作用于當(dāng)前類對(duì)象加鎖,進(jìn)入同步代碼前要獲得 當(dāng)前類對(duì)象的鎖
默認(rèn)使用當(dāng)前類的類名.class 鎖
public class ThreadCount implements Runnable {
private static Integer count = 100;
private static String lock = “l(fā)ock”;
@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死鎖問題
我們?nèi)绻谑褂胹ynchronized 需要注意 synchronized鎖嵌套的問題 避免死鎖的問題發(fā)生。
案例:
public class DeadlockThread implements Runnable {
private int count = 1;
private String lock = “l(fā)ock”;
@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 先獲取到自定義對(duì)象的lock鎖,進(jìn)入到a方法需要獲取this鎖
線程2 先獲取this鎖, 進(jìn)入到b方法需要自定義對(duì)象的lock鎖
線程1 線程2 是在同時(shí)執(zhí)行
線程1 線程2
先獲取到自定義對(duì)象的lock鎖 先獲取this鎖
需要線程2已經(jīng)持有的this鎖 線程1已經(jīng)持有自定義對(duì)象的lock鎖
總結(jié)
以上是生活随笔為你收集整理的synchronized锁的基本用法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。