java asynchronize_Java 中synchronize函数的实例详解
Java 中synchronize函數(shù)的實(shí)例詳解
java中的一個(gè)類(lèi)的成員函數(shù)若用synchronized來(lái)修飾,則對(duì)應(yīng)同一個(gè)對(duì)象,多個(gè)線程像調(diào)用這個(gè)對(duì)象的這個(gè)同步函數(shù)時(shí)必須等到上一個(gè)線程調(diào)用完才能由下一個(gè)線程調(diào)用。
那么如果一個(gè)類(lèi)同時(shí)有兩個(gè)成員函數(shù)是由synchronized修飾如代碼所示,對(duì)與同一個(gè)對(duì)象,是否可以在兩個(gè)線程運(yùn)行時(shí),一個(gè)調(diào)用funcA,同時(shí)另一個(gè)調(diào)用funcB?
Mysyn是這樣一個(gè)類(lèi),如果我有兩個(gè)線程,一個(gè)在run方法中先運(yùn)行funcA再運(yùn)行funcB,另一個(gè)線程在run方法中先運(yùn)行funcB再運(yùn)行funcA。那有沒(méi)有可能出現(xiàn)這樣的情況:在輸出時(shí)start A...后面直接輸出start B...?
public class MySyn {
public synchronized void funcA(String str){
System.out.println(str+":");
System.out.println("start A...");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("...A end");
}
public synchronized void funcB(String str){
System.out.println(str+":");
System.out.println("start B...");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("...B end");
}
}
測(cè)試代碼如下:
這個(gè)線程是先運(yùn)行funcA的
public class Mythread implements Runnable {
private MySyn mysyn;
private String id;
public Mythread(MySyn syn, String id){
this.mysyn = syn;
this.id = id;
}
@Override
public void run() {
this.mysyn.funcA(id);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.mysyn.funcB(id);
}
public static void main(String arg[]){
MySyn syn=new MySyn();
Thread t1 = new Thread(new Mythread(syn, "t1"));
Thread t2 = new Thread(new YourThread(syn, "t2"));
t1.start();
t2.start();
}
}
這個(gè)線程是先運(yùn)行funcB的
public class YourThread implements Runnable {
private MySyn mysyn;
private String id;
public YourThread(MySyn syn, String id){
this.mysyn = syn;
this.id=id;
}
@Override
public void run() {
this.mysyn.funcB(id);
this.mysyn.funcA(id);
}
}
輸出結(jié)果多是:
t1:
start A...
...A end
t2:
start B...
...B end
t2:
start A...
...A end
t1:
start B...
...B end
如果取消Mythread的run方法中兩個(gè)函數(shù)調(diào)用間的sleep,那結(jié)果多是:
t1:
start A...
...A end
t1:
start B...
...B end
t2:
start B...
...B end
t2:
start A...
...A end
個(gè)人結(jié)果可能因線程調(diào)度不同,但是永遠(yuǎn)不會(huì)有:start A...后面直接輸出start B...
那如果funcB不是一個(gè)同步函數(shù),那上述代碼運(yùn)行結(jié)果會(huì)是怎么樣呢?
代碼稍加改動(dòng),把funcB的synchronized關(guān)鍵字去掉。運(yùn)行結(jié)果為:
t2:
t1:
start A...
start B...
...A end
t1:
start B...
...B end
t2:
start A...
...B end
...A end
顯然出現(xiàn)了start A...后面直接輸出start B...的結(jié)果。
同樣如果Mysyn類(lèi)如果有一個(gè)public 的成員變量,多線程也可以再同步函數(shù)被調(diào)用的同時(shí),由另一個(gè)線程修改這個(gè)成員變量。
上述實(shí)驗(yàn)說(shuō)明了:同步的成員函數(shù)只能在同一個(gè)對(duì)象的同步函數(shù)調(diào)用中對(duì)其他同步函數(shù)(包括本身)有排它的效果,即多線程運(yùn)行中,同一個(gè)對(duì)象當(dāng)前只能有一個(gè)同步函數(shù)在運(yùn)行,但不排除其他非同步函數(shù)的運(yùn)行或?qū)Τ蓡T進(jìn)行訪問(wèn)。
那現(xiàn)在假設(shè)某個(gè)類(lèi)有兩個(gè)靜態(tài)同步方法,那情況怎么樣呢?
具體實(shí)現(xiàn),我就不重復(fù)了,因?yàn)榻Y(jié)果類(lèi)似:
在多線程中,同一個(gè)類(lèi),當(dāng)前只能有一個(gè)類(lèi)同步函數(shù)(靜態(tài)同步函數(shù))在運(yùn)行,但不排除其他非同步靜態(tài)函數(shù)的運(yùn)行或?qū)o態(tài)成員的訪問(wèn)
如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
總結(jié)
以上是生活随笔為你收集整理的java asynchronize_Java 中synchronize函数的实例详解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: php7.1安装mysqli扩展,cen
- 下一篇: 页表长度和页表大小_在请求调页系统中,若