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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 同步锁死锁,Android多线程研究(3)——线程同步和互斥及死锁

發布時間:2023/12/10 Android 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 同步锁死锁,Android多线程研究(3)——线程同步和互斥及死锁 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為什么會有線程同步的概念呢?為什么要同步?什么是線程同步?先看一段代碼:

package com.maso.test;

public class ThreadTest2 implements Runnable{

private TestObj testObj = new TestObj();

public static void main(String[] args) {

ThreadTest2 tt = new ThreadTest2();

Thread t1 = new Thread(tt, "thread_1");

Thread t2 = new Thread(tt, "thread_2");

t1.start();

t2.start();

}

@Override

public void run() {

for(int j = 0; j < 10; j++){

int i = fix(1);

try {

Thread.sleep(1);

} catch (InterruptedException e) {

e.printStackTrace();

}

System.out.println(Thread.currentThread().getName() + " : i = " + i);

}

}

public int fix(int y){

return testObj.fix(y);

}

public class TestObj{

int x = 10;

public int fix(int y){

return x = x - y;

}

}

}輸出結果后,就會發現變量x被兩個線程同時操作,這樣就很容易導致誤操作。如何才能解決這個問題呢?用線程的同步技術,加上synchronized關鍵字

public synchronized int fix(int y){

return testObj.fix(y);

}加上同步后,就可以看到有序的從9輸出到-10.

如果加到TestObj類的fix方法上能不能實現同步呢?

public class TestObj{

int x = 10;

public synchronized int fix(int y){

return x = x - y;

}

}如果將synchronized加到方法上則等價于

synchronized(this){

}可以判斷出兩個線程使用的TestObj類的同一個實例testOjb,所以后實現同步,但是輸出的結果卻不是理想的結果。這是因為當A線程執行完x = x - y后還沒有輸出則B線程已經進入開始執行x = x - y.

所以像下面這樣輸出就不會有什么問題了:

public class TestObj{

public TestObj(){

System.out.println("調用了構造函數");

}

int x = 10;

public synchronized int fix(int y){

x = x - y;

System.out.println(Thread.currentThread().getName() + " : x = " + x);

return x;

}

}如果將外部的fix方法修改如下:

public int fix(int y){

ax++ ;

if(ax%2 == 0){

return testObj.fix(y, testObj.str1);

}else{

return testObj.fix(y, testObj.str2);

}

}

public class TestObj{

String str1 = "a1";

String str2 = "a2";

public TestObj(){

System.out.println("調用了構造函數");

}

int x = 10;

public int fix(int y, String str){

synchronized (str) {

x = x - y;

System.out.println(Thread.currentThread().getName() + " : x = " + x);

}

return x;

}

}此時synchronized中的str對象不是同一個對象,所以兩個線程所持有的對象鎖不是同一個,這樣就不能實現同步。要實現線程之間的互斥就要使用同一個對象鎖。

什么是死鎖呢?舉個例子就是比如你和同學租了個兩室的房子,你拿著你房子的鑰匙,你同學拿著他房子的鑰匙,現在你在房子等你同學將他的鑰匙給你然后你進他房子,你同學在他的房子等你將鑰匙給他然后他進你的房子,這樣就死鎖了。

package com.maso.test;

public class ThreadDieSock implements Runnable {

private int flag = 1;

private Object obj1 = new Object(), obj2 = new Object();

public void run() {

System.out.println("flag=" + flag);

if (flag == 1) {

synchronized (obj1) {

System.out.println("我已經鎖定obj1,休息0.5秒后鎖定obj2去!");

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

synchronized (obj2) {

System.out.println("1");

}

}

}

if (flag == 0) {

synchronized (obj2) {

System.out.println("我已經鎖定obj2,休息0.5秒后鎖定obj1去!");

try {

Thread.sleep(500);

} catch (InterruptedException e) {

e.printStackTrace();

}

synchronized (obj1) {

System.out.println("0");

}

}

}

}

public static void main(String[] args) {

ThreadDieSock run01 = new ThreadDieSock();

ThreadDieSock run02 = new ThreadDieSock();

run01.flag = 1;

run02.flag = 0;

Thread thread01 = new Thread(run01);

Thread thread02 = new Thread(run02);

System.out.println("線程開始嘍!");

thread01.start();

thread02.start();

}

}

總結

以上是生活随笔為你收集整理的Android 同步锁死锁,Android多线程研究(3)——线程同步和互斥及死锁的全部內容,希望文章能夠幫你解決所遇到的問題。

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