第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

多線程中關(guān)于鎖問(wèn)題

多線程中關(guān)于鎖問(wèn)題

package?com.multi; public?class?ThreadTest?implements?Runnable?{ private?Integer?ticket?=?10; private?Integer?count?=?0; private?byte[]?lock?=?new?byte[0]; public?void?run()?{ synchronized?(ticket)?{ while?(true)?{ if?(ticket?<=?-100)?{ break; } System.out.println(String.format("thread:?%s?,?tiketnum?=?%d?do?some?thing" ,Thread.currentThread().getName() ,ticket--)); count++; } } } public?static?void?main(String[]?args)?{ ThreadTest?mTh1?=?new?ThreadTest(); Thread[]?th?=?new?Thread[5]; for?(int?i?=?0;?i?<?5;?i++)?{ th[i]=?new?Thread(mTh1,"th"+i); th[i].start(); } try?{ Thread.sleep(1500); }?catch?(InterruptedException?e)?{ //?TODO?Auto-generated?catch?block e.printStackTrace(); } System.out.println(mTh1.count); } } 執(zhí)行結(jié)果 thread:?th4?,?tiketnum?=?6?do?some?thing thread:?th3?,?tiketnum?=?7?do?some?thing thread:?th4?,?tiketnum?=?5?do?some?thing thread:?th3?,?tiketnum?=?4?do?some?thing thread:?th3?,?tiketnum?=?2?do?some?thing thread:?th4?,?tiketnum?=?3?do?some?thing thread:?th4?,?tiketnum?=?0?do?some?thing thread:?th4?,?tiketnum?=?-1?do?some?thing thread:?th4?,?tiketnum?=?-2?do?some?thing thread:?th4?,?tiketnum?=?-3?do?some?thing thread:?th4?,?tiketnum?=?-4?do?some?thing thread:?th4?,?tiketnum?=?-5?do?some?thing thread:?th4?,?tiketnum?=?-6?do?some?thing thread:?th4?,?tiketnum?=?-7?do?some?thing thread:?th4?,?tiketnum?=?-8?do?some?thing thread:?th1?,?tiketnum?=?9?do?some?thing …………………… …………java多線程中,鎖不起作用?
查看完整描述

4 回答

?
qq_青棗工作室_0

TA貢獻(xiàn)446條經(jīng)驗(yàn) 獲得超754個(gè)贊

synchronized (ticket) ,改為?synchronized (lock)?

查看完整回答
3 反對(duì) 回復(fù) 2016-05-10
?
竹馬君

TA貢獻(xiàn)64條經(jīng)驗(yàn) 獲得超115個(gè)贊

關(guān)鍵在
System.out.println(String.format("thread:?%s?,?tiketnum?=?%d?do?some?thing",Thread.currentThread().getName(),ticket--));

如果將ticket--移出來(lái),改寫成
System.out.println(String.format("thread:?%s?,?tiketnum?=?%d?do?some?thing",Thread.currentThread().getName(),ticket));
ticket--;
輸出就正常了
thread:?th0?,?tiketnum?=?10?do?some?thing
thread:?th0?,?tiketnum?=?9?do?some?thing
thread:?th0?,?tiketnum?=?8?do?some?thing
thread:?th0?,?tiketnum?=?7?do?some?thing
thread:?th0?,?tiketnum?=?6?do?some?thing
thread:?th0?,?tiketnum?=?5?do?some?thing
thread:?th0?,?tiketnum?=?4?do?some?thing
thread:?th0?,?tiketnum?=?3?do?some?thing
thread:?th0?,?tiketnum?=?2?do?some?thing
thread:?th0?,?tiketnum?=?1?do?some?thing
thread:?th0?,?tiketnum?=?0?do?some?thing
thread:?th0?,?tiketnum?=?-1?do?some?thing

查看完整回答
2 反對(duì) 回復(fù) 2016-05-10
  • 4 回答
  • 0 關(guān)注
  • 3298 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)