synchronized(this)涮总、synchronized(class)和synchronized(Object)的區(qū)別

在多線程開發(fā)中,我們經(jīng)车灰ǎ看到synchronized(this)瀑梗、synchronized(*.class)與synchronized(任意對象)這幾種類型同步方法烹笔。但是是否知道這幾種寫法有什么區(qū)別了?下面根據(jù)代碼來分析:

1抛丽、synchronized代碼塊間的同步性



運(yùn)行結(jié)果:

結(jié)論:

當(dāng)一個(gè)線程訪問ObjectService的一個(gè)synchronized (this)同步代碼塊時(shí)谤职,其它線程對同一個(gè)ObjectService中其它的synchronized (this)同步代碼塊的訪問將是堵塞,說明synchronized (this)使用的是同一個(gè)對象鎖亿鲜。



2允蜈、驗(yàn)證synchronized (this)代碼塊是鎖定當(dāng)前對象



package?com.zwz.thread.demo2;??

public?class?ObjectService?{ ?

public?void?objectMethodA(){??

System.out.println("run----objectMethodA");??

????}??

public?void?objectMethodB(){??

synchronized?(this)?{??

try?{??

for?(int?i?=?1;?i?<=?10;?i++)?{??

System.out.println("synchronized?thread?name:"+Thread.currentThread().getName()+"-->i="+i);??

Thread.sleep(1000);??

????????????????}??

}catch?(InterruptedException?e)?{??

????????????????????e.printStackTrace();??

????????????}??

????????}??

????}??

}??



package?com.zwz.thread.demo2; ?

public?class?ThreadA?extends?Thread?{??

private?ObjectService?objectService; ?

public?ThreadA(ObjectService?objectService)?{??

super();??

this.objectService?=?objectService;??

????}??

@Override??

public?void?run()?{??

super.run();??

????????objectService.objectMethodA();??

????}??

}??



package?com.zwz.thread.demo2; ?

public?class?ThreadB?extends?Thread?{??

private?ObjectService?objectService;??

public?ThreadB(ObjectService?objectService)?{ ?

super();??

this.objectService?=?objectService;??

????}??

@Override??

public?void?run()?{??

super.run();??

????????objectService.objectMethodB();??

????}??

}??



package?com.zwz.thread.demo2;??

public?class?MainTest?{ ?

public?static?void?main(String[]?args)?throws?InterruptedException?{??

ObjectService?service=new?ObjectService();??

ThreadB?b=new?ThreadB(service);??

????????b.start();??

Thread.sleep(2000);??

ThreadA?a=new?ThreadA(service);??

????????a.start();??

????}??

}??

運(yùn)行結(jié)果:

可以看到?jīng)]有同步鎖的objectMethodA方法異步執(zhí)行了,下面我們將objectMethodA()加上同步蒿柳。



package?com.zwz.thread.demo2;??

public?class?ObjectService?{ ?

public?synchronized?void?objectMethodA(){??

System.out.println("run----objectMethodA");??

????}??

public?void?objectMethodB(){??

synchronized?(this)?{??

try?{??

for?(int?i?=?1;?i?<=?10;?i++)?{??

System.out.println("synchronized?thread?name:"+Thread.currentThread().getName()+"-->i="+i);??

Thread.sleep(1000);??

????????????????}??

}catch?(InterruptedException?e)?{??

????????????????????e.printStackTrace();??

????????????}??

????????}??

????}??

}??



運(yùn)行結(jié)果:

結(jié)論:

上面三個(gè)小例子我們可以知道饶套,多個(gè)線程調(diào)用同一個(gè)對象中的不同名稱的synchronized同步方法或synchronized(this)同步代碼塊時(shí),是同步的垒探。

1妓蛮、synchronized同步方法

①對其它的synchronized同步方法或synchronized(this)同步代碼塊調(diào)用是堵塞狀態(tài);

②同一時(shí)間只有一個(gè)線程執(zhí)行synchronized同步方法中的代碼圾叼。

2蛤克、synchronized(this)同步代碼塊

①對其它的synchronized同步方法或synchronized(this)同步代碼塊調(diào)用是堵塞狀態(tài);

②同一時(shí)間只有一個(gè)線程執(zhí)行synchronized同步方法中的代碼夷蚊。



3构挤、將任意對象作為對象監(jiān)視器



package?com.zwz.thread.demo3; ?

public?class?ObjectService?{??

private?String?uname;??

private?String?pwd;??

String?lock=new?String();??

public?void?setUserNamePassWord(String?userName,String?passWord){??

try?{??

synchronized?(lock)?{??

System.out.println("thread?name="+Thread.currentThread().getName()??

+"?進(jìn)入代碼快:"+System.currentTimeMillis());??

????????????????uname=userName;??

Thread.sleep(3000);??

????????????????pwd=passWord;??

System.out.println("thread?name="+Thread.currentThread().getName()??

+"?進(jìn)入代碼快:"+System.currentTimeMillis()+"入?yún)name:"+uname+"入?yún)wd:"+pwd);??

????????????}??

}catch?(InterruptedException?e)?{??

????????????e.printStackTrace();??

????????}??

????}??

}??



package?com.zwz.thread.demo3; ?

public?class?ThreadA?extends?Thread?{??

private?ObjectService?objectService; ?

public?ThreadA(ObjectService?objectService)?{??

super();??

this.objectService?=?objectService;??

????}??

@Override??

public?void?run()?{??

objectService.setUserNamePassWord("a",?"aa");??

????}??

}??



package?com.zwz.thread.demo3; ?

public?class?ThreadB?extends?Thread?{??

private?ObjectService?objectService; ?

public?ThreadB(ObjectService?objectService)?{??

super();??

this.objectService?=?objectService;??

????}??

@Override??

public?void?run()?{??

objectService.setUserNamePassWord("b",?"bb");??

????}??

}??



package?com.zwz.thread.demo3; ?

public?class?MainTest?{??

public?static?void?main(String[]?args)?{??

ObjectService?service=new?ObjectService();??

ThreadA?a=new?ThreadA(service);??

a.setName("A");??

????????a.start();??

ThreadB?b=new?ThreadB(service);??

b.setName("B");??

????????b.start();??

????}??

}??



運(yùn)行結(jié)果:

下面我把String lock=new String();放在方法中:



package?com.zwz.thread.demo3;??

public?class?ObjectService?{ ?

private?String?uname;??

private?String?pwd;??

public?void?setUserNamePassWord(String?userName,String?passWord){??

try?{??

String?lock=new?String();??

synchronized?(lock)?{??

System.out.println("thread?name="+Thread.currentThread().getName()??

+"?進(jìn)入代碼快:"+System.currentTimeMillis());??

????????????????uname=userName;??

Thread.sleep(3000);??

????????????????pwd=passWord;??

System.out.println("thread?name="+Thread.currentThread().getName()??

+"?進(jìn)入代碼快:"+System.currentTimeMillis()+"入?yún)name:"+uname+"入?yún)wd:"+pwd);??

????????????}??

}catch?(InterruptedException?e)?{??

????????????e.printStackTrace();??

????????}??

????}??



運(yùn)行結(jié)果:

結(jié)論:

多個(gè)線程持有對象監(jiān)視器作為同一個(gè)對象的前提下,同一時(shí)間只有一個(gè)線程可以執(zhí)行synchronized(任意自定義對象)同步代碼快惕鼓。



4筋现、synchronized(任意自定義對象)與synchronized同步方法共用



package?com.zwz.thread.demo4;??

public?class?ObjectService?{ ?

private?String?lock=new?String();??

public?void?methodA(){??

try?{??

synchronized?(lock)?{??

System.out.println("a?begin");??

Thread.sleep(3000);??

System.out.println("a???end");??

????????????}??

}catch?(InterruptedException?e)?{??

????????????e.printStackTrace();??

????????}??

????}??

public?synchronized?void?methodB(){??

System.out.println("b?begin");??

System.out.println("b???end");??

????}??

}??



package?com.zwz.thread.demo4;??

public?class?ThreadA?extends?Thread?{ ?

private?ObjectService?objectService; ?

public?ThreadA(ObjectService?objectService)?{??

super();??

this.objectService?=?objectService;??

????}??

@Override??

public?void?run()?{??

????????objectService.methodA();??

????}??



package?com.zwz.thread.demo4;??

public?class?ThreadB?extends?Thread?{ ?

private?ObjectService?objectService; ?

public?ThreadB(ObjectService?objectService)?{??

super();??

this.objectService?=?objectService;??

????}??

@Override??

public?void?run()?{??

????????objectService.methodB();??

????}??

}??



package?com.zwz.thread.demo4; ?

public?class?MainTest?{??

public?static?void?main(String[]?args)?{??

ObjectService?service=new?ObjectService();??

ThreadA?a=new?ThreadA(service);??

a.setName("A");??

????????a.start();??

ThreadB?b=new?ThreadB(service);??

b.setName("B");??

????????b.start();??

????} ?

}??



運(yùn)行結(jié)果:

結(jié)論:

上面methodA持有l(wèi)ock對象鎖,methodB持有ObjectService對象鎖呜笑,不是同一個(gè)夫否,所以使用synchronized(任意自定義對象)進(jìn)行同步操作,對象監(jiān)視器必須是同一個(gè)對象叫胁。如果不是同一個(gè)凰慈,運(yùn)行就是異步執(zhí)行了。

5驼鹅、靜態(tài)同步synchronized方法



package?com.zwz.thread.demo5; ?

public?class?ObjectService?{??

public?synchronized?static?void?methodA(){??

try?{??

System.out.println("static?methodA?begin?線程名稱:"+Thread.currentThread().getName()+"?times:"+System.currentTimeMillis());??

Thread.sleep(3000);??

System.out.println("static?methodA?end???線程名稱:"+Thread.currentThread().getName()+"?times:"+System.currentTimeMillis());??

}catch?(InterruptedException?e)?{??

????????????e.printStackTrace();??

????????}??

????}??


public?synchronized?static?void?methodB(){??

System.out.println("static?methodB?begin?線程名稱:"+Thread.currentThread().getName()+"?times:"+System.currentTimeMillis());??

System.out.println("static?methodB?end???線程名稱:"+Thread.currentThread().getName()+"?times:"+System.currentTimeMillis());??

????}??

}??



package?com.zwz.thread.demo5;??

public?class?ThreadA?extends?Thread?{ ?

@Override ?

public?void?run()?{??

????????ObjectService.methodA();??

????}??

}??



package?com.zwz.thread.demo5; ?

public?class?ThreadB?extends?Thread?{??

@Override??

public?void?run()?{??

????????ObjectService.methodB();??

????}??

}??



package?com.zwz.thread.demo5; ?

public?class?MainTest?{??

public?static?void?main(String[]?args)?{??

ThreadA?a=new?ThreadA();??

a.setName("A");??

????????a.start();??

ThreadB?b=new?ThreadB();??

b.setName("B");??

????????b.start();??

????}??

}??



運(yùn)行結(jié)果:

結(jié)論:

synchronized應(yīng)用在static方法上微谓,那是對當(dāng)前對應(yīng)的*.Class進(jìn)行持鎖。



6输钩、synchronized(*.class)代碼塊



package?com.zwz.thread.demo6; ?

public?class?ObjectService?{??

public?void?methodA(){??

try?{??

synchronized?(ObjectService.class)?{??

System.out.println("methodA?begin?線程名稱:"+Thread.currentThread().getName()+"?times:"+System.currentTimeMillis());??

Thread.sleep(3000);??

System.out.println("methodA?end???線程名稱:"+Thread.currentThread().getName()+"?times:"+System.currentTimeMillis());??

????????????}??

}catch?(InterruptedException?e)?{??

????????????e.printStackTrace();??

????????}??

????}??


public?void?methodB(){??

synchronized?(ObjectService.class)?{??

System.out.println("methodB?begin?線程名稱:"+Thread.currentThread().getName()+"?times:"+System.currentTimeMillis());??

System.out.println("methodB?end???線程名稱:"+Thread.currentThread().getName()+"?times:"+System.currentTimeMillis());??

????????}??

????}??

}??



package?com.zwz.thread.demo6;??

public?class?ThreadA?extends?Thread?{ ?

private?ObjectService?objectService; ?

public?ThreadA(ObjectService?objectService)?{??

super();??

this.objectService?=?objectService;??

????}??

@Override??

public?void?run()?{??

????????objectService.methodA();??

????}??

}??



package?com.zwz.thread.demo6; ?

public?class?ThreadB?extends?Thread?{??

private?ObjectService?objectService;??

public?ThreadB(ObjectService?objectService)?{ ?

super();??

this.objectService?=?objectService;??

????}??

@Override??

public?void?run()?{??

????????objectService.methodB();??

????}??

}??



package?com.zwz.thread.demo6; ?

public?class?MainTest?{??

public?static?void?main(String[]?args)?{??

ObjectService?service=new?ObjectService();??

ThreadA?a=new?ThreadA(service);??

a.setName("A");??

????????a.start();??

ThreadB?b=new?ThreadB(service);??

b.setName("B");??

????????b.start();??

????}??

}??



運(yùn)行結(jié)果:

結(jié)論:

同步synchronized(*.class)代碼塊的作用其實(shí)和synchronized static方法作用一樣豺型。Class鎖對類的所有對象實(shí)例起作用。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末买乃,一起剝皮案震驚了整個(gè)濱河市姻氨,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌剪验,老刑警劉巖肴焊,帶你破解...
    沈念sama閱讀 211,123評論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件前联,死亡現(xiàn)場離奇詭異,居然都是意外死亡娶眷,警方通過查閱死者的電腦和手機(jī)似嗤,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,031評論 2 384
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來届宠,“玉大人烁落,你說我怎么就攤上這事⊥阕ⅲ” “怎么了伤塌?”我有些...
    開封第一講書人閱讀 156,723評論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長幌羞。 經(jīng)常有香客問我寸谜,道長,這世上最難降的妖魔是什么属桦? 我笑而不...
    開封第一講書人閱讀 56,357評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮他爸,結(jié)果婚禮上聂宾,老公的妹妹穿的比我還像新娘。我一直安慰自己诊笤,他們只是感情好系谐,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,412評論 5 384
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著讨跟,像睡著了一般纪他。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上晾匠,一...
    開封第一講書人閱讀 49,760評論 1 289
  • 那天茶袒,我揣著相機(jī)與錄音,去河邊找鬼凉馆。 笑死薪寓,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的澜共。 我是一名探鬼主播向叉,決...
    沈念sama閱讀 38,904評論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼嗦董!你這毒婦竟也來了母谎?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,672評論 0 266
  • 序言:老撾萬榮一對情侶失蹤京革,失蹤者是張志新(化名)和其女友劉穎奇唤,沒想到半個(gè)月后供璧,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,118評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡冻记,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,456評論 2 325
  • 正文 我和宋清朗相戀三年睡毒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片冗栗。...
    茶點(diǎn)故事閱讀 38,599評論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡演顾,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出隅居,到底是詐尸還是另有隱情钠至,我是刑警寧澤,帶...
    沈念sama閱讀 34,264評論 4 328
  • 正文 年R本政府宣布胎源,位于F島的核電站棉钧,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏涕蚤。R本人自食惡果不足惜宪卿,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,857評論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望万栅。 院中可真熱鬧佑钾,春花似錦、人聲如沸烦粒。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,731評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽扰她。三九已至兽掰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間徒役,已是汗流浹背孽尽。 一陣腳步聲響...
    開封第一講書人閱讀 31,956評論 1 264
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留廉涕,地道東北人泻云。 一個(gè)月前我還...
    沈念sama閱讀 46,286評論 2 360
  • 正文 我出身青樓,卻偏偏與公主長得像狐蜕,于是被迫代替她去往敵國和親宠纯。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,465評論 2 348

推薦閱讀更多精彩內(nèi)容

  • 文章來源:http://www.54tianzhisheng.cn/2017/06/04/Java-Thread/...
    beneke閱讀 1,473評論 0 1
  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法层释,類相關(guān)的語法婆瓜,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法廉白,線程的語...
    子非魚_t_閱讀 31,598評論 18 399
  • Lock和synchronized synchronized都知道是用于同步代碼塊和方法的个初,線程一旦獲得對象鎖,其...
    耳_總閱讀 430評論 0 1
  • 皎皎云漢猴蹂,寂寂庭軒院溺,獨(dú)倚闌干。 風(fēng)吹簾動(dòng)處磅轻,寸寸愁腸酒一杯珍逸。 獨(dú)酌欲邀明月,惆悵不見團(tuán)團(tuán)聋溜。 覺今是昨非谆膳,落落更無語。
    貓貓葵閱讀 357評論 0 1
  • 漳州撮躁,素有花果之城漱病,魚米之鄉(xiāng)的美稱,被譽(yù)福建瓜果之鄉(xiāng)把曼!走進(jìn)漳州杨帽,還沒到站我就從動(dòng)車窗外的果園風(fēng)景已誘惑了我的心扉!...
    濰坊申通小蜜蜜閱讀 1,407評論 1 1