標籤:override null 應用 一個 trace read dex sys ota
同步鎖分類對象鎖(this)類鎖(類的位元組碼檔案對象即類名.class)字串鎖(比較特別)應用情境
在多線程下對共用資源的安全操作。
需求:啟動5個線程對共用資源total進行安全操作。
同步鎖在多線程單例模式下的使用
以上三類同步鎖都可以。
|
package cn.myThread;
public class MyThread implements Runnable {
private static int total = 10;
@Override
public void run() {
synchronized (this){ //使用this對象鎖
//synchronized (MyThread.class){ //使用MyThread.class類鎖
//synchronized (""){//使用字串鎖
System.out.println(Thread.currentThread().getName() + "正在運行");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
total--;
System.out.println(total);
System.out.println(Thread.currentThread().getName() + "線程結束");
}
}
} |
|
package cn.test;
import cn.myThread.MyThread;
public class TestMyThread {
public static void main(String[] args){
MyThread myThread = new MyThread();
Thread thread = null;
for (int i = 1 ; i <= 5 ; i++){
thread = new Thread(myThread,"線程"+i); //開啟5個線程,傳入同一個對象
thread.start();
}
}
} |
|
線程1正在運行
9
線程1線程結束
線程3正在運行
8
線程3線程結束
線程5正在運行
7
線程5線程結束
線程2正在運行
6
線程2線程結束
線程4正在運行
5
線程4線程結束 |
分析:從運行結果可以看出5個線程串列執行同步鎖裡面的代碼,因為5個線程中的同步鎖對象this指向同一個的對象(同步鎖對象MyThread.class類鎖是同一個對象、同步鎖對象 ”” 字串鎖是同一個對象),所以5個線程會串列執行同步鎖裡面的代碼。
同步鎖在多線程多例模式下的使用錯誤用法
|
package cn.myThread;
public class MyThread implements Runnable {
private static int total = 10;
@Override
public void run() {
synchronized (this){//使用this對象鎖
System.out.println(Thread.currentThread().getName() + "正在運行");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
total--;
System.out.println(total);
System.out.println(Thread.currentThread().getName() + "線程結束");
}
}
} |
|
package cn.test;
import cn.myThread.MyThread;
public class TestMyThread {
public static void main(String[] args){
Thread thread = null;
for (int i = 1 ; i <= 5 ; i++){
thread = new Thread(new MyThread(),"線程"+i);//開啟5個線程,傳入5個不同對象
thread.start();
}
}
} |
|
線程2正在運行
線程1正在運行
線程3正在運行
線程5正在運行
線程4正在運行
9
7
9
8
線程1線程結束
線程5線程結束
線程2線程結束
線程3線程結束
6
線程4線程結束 |
分析:從運行結果可以看出5個線程並存執行同步鎖裡面的代碼,因為5個線程中的同步鎖對象this指向5個不同的對象,所以5個線程會同時執行同步鎖裡面的代碼。
正確用法方式一:
|
package cn.myThread;
public class MyThread implements Runnable {
private static int total = 10;
@Override
public void run() {
synchronized (MyThread.class){//使用MyThread.class類鎖
System.out.println(Thread.currentThread().getName() + "正在運行");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
total--;
System.out.println(total);
System.out.println(Thread.currentThread().getName() + "線程結束");
}
}
} |
|
package cn.test;
import cn.myThread.MyThread;
public class TestMyThread {
public static void main(String[] args){
Thread thread = null;
for (int i = 1 ; i <= 5 ; i++){
thread = new Thread(new MyThread(),"線程"+i); //開啟5個線程,傳入5個不同對象
thread.start();
}
}
} |
|
線程1正在運行
9
線程1線程結束
線程5正在運行
8
線程5線程結束
線程4正在運行
7
線程4線程結束
線程3正在運行
6
線程3線程結束
線程2正在運行
5
線程2線程結束 |
分析:從運行結果可以看出5個線程串列執行同步鎖裡面的代碼,因為5個線程中的同步鎖對象MyThread.class類鎖是同一個對象,所以5個線程會串列執行同步鎖裡面的代碼。
方式二:
|
package cn.myThread;
public class MyThread implements Runnable {
private static int total = 10;
@Override
public void run() {
synchronized (""){//使用字串鎖
System.out.println(Thread.currentThread().getName() + "正在運行");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
total--;
System.out.println(total);
System.out.println(Thread.currentThread().getName() + "線程結束");
}
}
} |
|
package cn.test;
import cn.myThread.MyThread;
public class TestMyThread {
public static void main(String[] args){
Thread thread = null;
for (int i = 1 ; i <= 5 ; i++){
thread = new Thread(new MyThread(),"線程"+i); //開啟5個線程,傳入5個不同對象
thread.start();
}
}
} |
|
線程1正在運行
9
線程1線程結束
線程4正在運行
8
線程4線程結束
線程5正在運行
7
線程5線程結束
線程3正在運行
6
線程3線程結束
線程2正在運行
5
線程2線程結束 |
分析:從運行結果可以看出5個線程串列執行同步鎖裡面的代碼,因為5個線程中的同步鎖對象 ”” 字串鎖是同一個對象,所以5個線程會串列執行同步鎖裡面的代碼。
java同步鎖的正確使用