鍓嶈█
瀵逛簬绋嬪簭鍛樻潵璇达紝绾跨▼涓€鐩存槸鎴戜滑寮€鍙戜腑鏈€甯稿嚭鐜扮殑鎶€鏈紝鍙互璇达紝浣跨敤璧锋潵瀹屽叏娌¢棶棰橈紝閫氳繃鐧惧害浠ュ強鐔熸倝搴﹀彲浠ラ『鎵嬫媹鏉ワ紝浣嗘槸瀵逛簬娣卞叆鐞嗚В锛屽嵈涓嶆槸鎵€鏈変汉閮借兘鍋氬埌锛屽啓杩欑瘒鏂囩珷鐨勭洰鐨勶紝涓昏鐢ㄤ簬鑷繁杩涜澶嶄範锛屾€荤粨锛屾湭鏉ヤ篃浼氭寔缁慨鏀硅鏂囷紝鐩墠锛屼綔鑰呭澶氱嚎绋嬪苟涓嶆繁鍏ワ紝鍥犳鏂囦腑鎽樻妱浜嗗緢澶氬ぇ浣殑涓€浜涙枃绔狅紝鎰熻阿澶т浆浠殑寮€婧愩€傛枃涓檮鏈夌浉鍏抽摼鎺ワ紝鍙嚜琛岃烦杞紝鎰熻阿鍛︼紒锛?/p>
馃憮 绾跨▼銆佽繘绋?/h2>
馃殫 杩涚▼
鎸囧湪绯荤粺涓鍦ㄨ繍琛岀殑涓€涓簲鐢ㄧ▼搴忥紱绋嬪簭涓€鏃﹁繍琛屽氨鏄?strong>杩涚▼锛涙槸绯荤粺杩涜璧勬簮鍒嗛厤鐨勫熀鏈崟浣?/p>
馃Х 绾跨▼
杩涚▼涔嬪唴鐙珛鎵ц鐨勪竴涓崟鍏冩墽琛屾祦銆?strong>绾跨▼鏄?code>CPU璋冨害鍜屾墽琛岀殑鏈€灏忓崟浣嶏紝鍖呭惈鍦ㄨ繘绋嬩箣涓紝鏄繘绋嬩腑鐨勫疄闄呰繍浣滃崟浣嶃€備竴鏉$嚎绋嬫寚鐨勬槸杩涚▼涓竴涓崟涓€椤哄簭鐨勬帶鍒舵祦锛屼竴涓繘绋嬩腑鍙互骞跺彂澶氫釜绾跨▼锛屾瘡鏉?strong>绾跨▼骞惰鎵ц涓嶅悓鐨勪换鍔°€?/p>
馃憮 涓轰粈涔堜娇鐢ㄥ绾跨▼
- 涓荤嚎绋?/strong>涓嶈兘鎵ц鑰楁椂杈冮暱鐨勪换鍔★紝鍚﹀垯浼氶樆濉?strong>UI绾跨▼锛屽紩璧?code>ANR銆佸崱椤跨瓑闂锛堝彧鑳藉湪UI绾跨▼鎿嶄綔UI瑙嗗浘锛屼笉鑳藉湪瀛愮嚎绋嬩腑鎿嶄綔锛?/li>
- Android 寮哄埗瑕佹眰寮€鍙戣€呭湪鍙戣捣缃戠粶璇锋眰鏃讹紝蹇呴』鍦ㄥ伐浣滅嚎绋嬶紝涓嶈兘鍦ㄤ富绾跨▼锛屽惁鍒欐姏鍑?code>NetworkOnMainThreadException
馃憮 澶氱嚎绋嬪満鏅?/h2>
- 鍦?strong>Android涓紝App浠庝竴鍚姩銆傚氨绠楁槸涓€涓┖鐧絛emo锛屽畠涔熸槸澶氱嚎绋嬪簲鐢紝鍘熷洜鏄洜涓篈pp鏄繍琛屽湪
art
涓婏紝art
鑷甫GC
绾跨▼锛屽啀鍔犱笂App蹇呮湁鐨?strong>涓荤嚎绋嬶紙UI绾跨▼锛夛紝灏辩粍鎴愪簡涓€涓绾跨▼搴旂敤
- 鏃ュ父浣跨敤鐨勪笁鏂瑰簱锛屾瘮濡?code>Okhttp銆丟lide銆丷xJava
- 澶勭悊鑰楁椂浠诲姟锛屽垹闄ゆ暟鎹紝娓呯┖缂撳瓨锛屾搷浣滄暟鎹簱绛夌瓑
UI 绾跨▼涓轰粈涔堜笉浼氱粨鏉燂紵鍥犱负瀹冨湪鍒濆鍖栧畬姣曞悗浼氭墽饩忔寰幆锛屽惊鐜殑鍐呭鏄埛鏂扮晫饩?/p>
馃憮 骞跺彂鍜屽苟琛?/h2>
- **骞跺彂鍜屽苟琛屾渶寮€濮嬮兘鏄搷浣滅郴缁熶腑鐨勬蹇碉紝琛ㄧず鐨勬槸CPU鎵ц澶氫釜浠诲姟鐨勬柟寮忋€傝繖涓や釜姒傚康鏋佸鏄撴贩娣嗐€?/li>
- 濡傛灉浣跨敤鐨勬槸鍗曟牳CPU锛岄偅涔堟垜浠槸鏃犳硶鎵ц骞惰鎿嶄綔鐨勶紝鍙兘鍋氬埌骞跺彂锛岃繖鏍锋潵鍏呭垎璋冪敤CPU鐨勮祫婧愩€?/li>
- 濡傛灉鎴戜滑浣跨敤鐨勬槸澶氭牳CPU锛屾垜浠墠鍙互鐪熸鐨勬剰涔変笂鍋氬埌骞惰鎿嶄綔銆?/li>
骞跺彂锛圕oncurrent锛夛紝鍦ㄦ搷浣滅郴缁熶腑锛屾槸鎸囦竴涓椂闂存涓湁鍑犱釜绋嬪簭閮藉浜庡凡鍚姩杩愯鍒拌繍琛屽畬姣曚箣闂达紝涓旇繖鍑犱釜绋嬪簭閮芥槸鍦ㄥ悓涓€涓鐞嗘満涓婅繍琛屻€?/strong>鎿嶄綔绯荤粺鐨勬椂闂寸墖鍒嗘椂璋冨害銆傛墦娓告垙鍜屽惉闊充箰涓や欢浜嬫儏鍦?strong>鍚屼竴涓椂闂存鍐?/strong>閮芥槸鍦ㄥ悓涓€鍙扮數鑴戜笂瀹屾垚浜?strong>浠庡紑濮嬪埌缁撴潫鐨勫姩浣?/strong>銆傞偅涔堬紝灏卞彲浠ヨ鍚煶涔愬拰鎵撴父鎴忔槸骞跺彂鐨勩€?/p>
骞惰锛圥arallel锛夛紝褰撶郴缁烠Pu鏈変竴涓互涓婄殑鍐呮牳鏃讹紝褰撲竴涓唴鏍告墽琛屼竴涓繘绋嬫椂锛屽彟涓€涓唴鏍稿彲浠ユ墽琛屽彟涓€涓繘绋嬶紝涓や釜杩涚▼浜掍笉鎶㈠崰鍐呮牳璧勬簮锛屽彲浠ュ悓鏃惰繘琛岋紝杩欑鏂瑰紡鎴戜滑绉颁箣涓哄苟琛?Parallel)銆?/strong>
婕旂ず鍦ㄥ悓涓€涓▼搴忎腑锛屽苟鍙戣窡骞惰鍙戠敓鐨勫満鏅?/p>
public class TestDemo {
public static void main(String[] args) {
//骞跺彂锛堟姠鍗犲叡鏈夎祫婧愶級
TestThread testThread = new TestThread();
Thread thread = new Thread(testThread, "thread-01");
Thread thread1 = new Thread(testThread, "thread-02");
Thread thread2 = new Thread(testThread, "thread-03");
Thread thread3 = new Thread(testThread, "thread-04");
thread.start();
thread1.start();
thread2.start();
thread3.start();
//骞惰(浜掔浉涓嶆姠鍗犺祫婧?
TestThread testThread1 = new TestThread();
TestThread testThread2 = new TestThread();
testThread1.start();
testThread2.start();
}
static class TestThread extends Thread {
public TestThread() { super("TestThread"); }
private int count = 10;
@Override
public void run() {
super.run();
System.out.println(count--);
}
}
}
馃憮 寮傛鍜屽悓姝?/h2>
鍚屾鍜屽紓姝ュ叧娉ㄧ殑鏄秷鎭€氫俊鏈哄埗.
鍚屾鏄寚: 鍙戦€佹柟鍙戝嚭鏁版嵁鍚? 绛夊緟鎺ユ敹鏂瑰彂鍥炲搷搴斿悗鎵嶅彂涓嬩竴涓暟鎹寘鐨勯€氳鏂瑰紡. 灏辨槸鍦ㄥ彂鍑轰竴涓皟鐢ㄦ椂, 鍦ㄦ病鏈夊緱鍒扮粨鏋滀箣鍓? 璇ヨ皟鐢ㄥ氨涓嶈繑鍥? 浣嗘槸涓€鏃﹁皟鐢ㄨ繑鍥? 灏卞緱鍒拌繑鍥炲€间簡. 涔熷氨鏄敱"璋冪敤鑰?涓诲姩绛夊緟杩欎釜"璋冪敤"鐨勭粨鏋?
寮傛鏄寚: 鍙戦€佹柟鍙戝嚭鏁版嵁鍚? 涓嶇瓑寰呮帴鏀舵柟鍙戝洖鍝嶅簲, 鎺ョ潃鍙戦€佷笅涓暟鎹寘鐨勯€氳鏂瑰紡. 褰撲竴涓紓姝ヨ繃绋嬭皟鐢ㄥ彂鍑哄悗, 璋冪敤鑰呬笉浼氱珛鍒诲緱鍒扮粨鏋? 鑰屾槸鍦ㄨ皟鐢ㄥ彂鍑哄悗, "琚皟鐢ㄨ€?閫氳繃鐘舵€併€侀€氱煡鏉ラ€氱煡璋冪敤鑰? 鎴栭€氳繃鍥炶皟鍑芥暟澶勭悊杩欎釜璋冪敤.
馃憮 浣跨敤绾跨▼鐨勫嚑绉嶆柟寮?/h2>
馃憮 Java
馃幆 Thread
Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
System.out.println("鐩存帴new鍑烘潵锛岀畝鍗曠矖鏆?);
}
};
thread1.start();
馃幆 Thread+Runnable
Runnable target = new Runnable() {
@Override
public void run() {
System.out.println("瀹炰緥涓€涓猺unnable瀵硅薄锛屼氦鐢辩嚎绋嬩娇鐢紝鏂逛究澶嶇敤");
}
};
Thread thread = new Thread(target);
Thread thread2 = new Thread(target);
thread.start();
thread2.start();
馃憮 ThreadFactory+Runnable
ThreadFactory threadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
return new Thread(runnable, "Thread-" + new Random().nextInt(1000));
}
};
Runnable target1 = new Runnable() {
@Override
public void run() {
System.out.println("瀹炰緥涓€涓猺unnable瀵硅薄锛屼氦鐢辩嚎绋嬪伐鍘備娇鐢紝寰楀埌thread瀵硅薄锛屾柟渚垮鐢?);
}
};
threadFactory.newThread(target1);
馃幆 ExecutorService+Runnable
Executor executor = Executors.newSingleThreadExecutor();
Executor executor1 = Executors.newCachedThreadPool();
Executor executor2 = Executors.newFixedThreadPool(10);
Executor executor3 = Executors.newScheduledThreadPool(1);
Runnable target2 = new Runnable() {
@Override
public void run() {
System.out.println("瀹炰緥涓€涓猺unnable瀵硅薄锛屼氦鐢辩嚎绋嬫睜浣跨敤锛岀嚎绋嬫睜甯姪闆嗕腑绠$悊绾跨▼锛岄伩鍏嶈祫婧愭氮璐癸紝鏂逛究澶嶇敤锛?);
}
};
executor.execute(target2);
executor1.execute(target2);
executor2.execute(target2);
executor3.execute(target2);
Future+Callable
Callable<String> callable = new Callable<String>() {
@Override
public String call() {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "鎻愪氦涓€涓换鍔″埌绾跨▼姹犻噷闈㈠幓!";
}
};
ExecutorService executor4 = Executors.newCachedThreadPool();
Future<String> future = executor4.submit(callable);
try {
String result = future.get();
System.out.println("result: " + result);
} catch (InterruptedException | ExecutionException e)
{
e.printStackTrace();
}
馃憮 android涓撳睘
- Handler
- IntentService
- HandlerThread
- AsyncTask
- Rxjava
鑰冭檻鍒扮瘒骞呬互鍙婅繖浜涘紓姝ユ満鍒堕兘鏄ぇ瀹舵瘮杈冪啛鐭ョ殑锛屾殏涓嶅垪鍑轰娇鐢ㄦ柟寮忥紝璐翠笂澶т浆浠啓杩囩殑鏂囩珷閾炬帴锛屼緵澶у浜嗚В锛屽鏋滃悗缁湁闇€瑕佸湪杩涜鍒楀嚭锛屼笉鐒剁瘒骞呭お闀匡紝鐪嬬潃鐪嬬潃寰堝鏄撴伓蹇?/p>
馃憮 kotlin
- Coroutine
馃憮 绾跨▼瀹夊叏
馃憮 涓轰粈涔堜細鍑虹幇绾跨▼瀹夊叏闂
- Java 鍐呭瓨妯″瀷瑙勫畾浜嗘墍鏈夌殑鍙橀噺閮藉瓨鍌ㄥ湪涓诲唴瀛樹腑锛屾瘡鏉$嚎绋嬫湁鑷繁鐨勫伐浣滃唴瀛樸€?/li>
- 绾跨▼鐨勫伐浣滃唴瀛樹腑淇濆瓨浜嗚绾跨▼涓敤鍒扮殑鍙橀噺鐨勪富鍐呭瓨鍓湰鎷疯礉锛岀嚎绋嬪鍙橀噺鐨勬墍鏈夋搷浣滈兘蹇呴』鍦ㄥ伐浣滃唴瀛樹腑杩涜锛岃€屼笉鑳界洿鎺ヨ鍐欎富鍐呭瓨銆?/li>
- 绾跨▼璁块棶涓€涓彉閲忥紝棣栧厛灏嗗彉閲忎粠涓诲唴瀛樻嫹璐濆埌宸ヤ綔鍐呭瓨锛屽鍙橀噺鐨勫啓鎿嶄綔锛屼笉浼氶┈涓婂悓姝ュ埌涓诲唴瀛樸€?/li>
- 涓嶅悓鐨勭嚎绋嬩箣闂翠篃鏃犳硶鐩存帴璁块棶瀵规柟宸ヤ綔鍐呭瓨涓殑鍙橀噺锛岀嚎绋嬮棿鍙橀噺鐨勪紶閫掑潎闇€瑕佽嚜宸辩殑宸ヤ綔鍐呭瓨鍜屼富瀛樹箣闂磋繘琛屾暟鎹悓姝ヨ繘琛屻€?/li>
馃幆 瑙e喅鏂瑰紡
瑙e喅鏂瑰紡閮ㄥ垎鎽樻妱鑷潰璇曞畼锛氳璇村绾跨▼骞跺彂闂
鏈汉瀵逛簬澶氱嚎绋嬬浉鐭ヤ笉澶氾紝鐩墠涔熷湪瀛︿範涓紝璇ラ儴鍒嗘殏鏃跺紩鐢紝绛夊悗缁細鍔犲叆鑷繁鐨勮瑙h繘琛屾敼鍐?/p>
- 淇濊瘉鍏变韩璧勬簮鍦ㄥ悓涓€鏃堕棿鍙兘鐢变竴涓嚎绋嬭繘琛屾搷浣?鍘熷瓙鎬э紝鏈夊簭鎬?銆?/li>
- 灏嗙嚎绋嬫搷浣滅殑缁撴灉鍙婃椂鍒锋柊锛屼繚璇佸叾浠栫嚎绋嬪彲浠ョ珛鍗宠幏鍙栧埌淇敼鍚庣殑鏈€鏂版暟鎹紙鍙鎬э級銆?/li>
馃幆 volatile
- 淇濊瘉鍙鎬э紝涓嶄繚璇佸師瀛愭€?/li>
- 褰撳啓涓€涓?code>volatile鍙橀噺鏃讹紝
JVM
浼氭妸鏈湴鍐呭瓨鐨勫彉閲忓己鍒跺埛鏂板埌涓诲唴瀛樹腑
- 杩欎釜鍐欐搷浣滃鑷村叾浠栫嚎绋嬩腑鐨勭紦瀛樻棤鏁堬紝鍏朵粬绾跨▼璇伙紝浼氫粠涓诲唴瀛樿銆?code>volatile鐨勫啓鎿嶄綔瀵瑰叾瀹冪嚎绋嬪疄鏃跺彲瑙併€?/li>
- 绂佹鎸囦护閲嶆帓搴?/li>
- 涓嶄細瀵瑰瓨鍦ㄤ緷璧栧叧绯荤殑鎸囦护閲嶆帓搴忥紝渚嬪 a = 1;b = a; a 鍜宐瀛樺湪渚濊禆鍏崇郴锛屼笉浼氳閲嶆帓搴?/li>
- 涓嶈兘褰卞搷鍗曠嚎绋嬩笅鐨勬墽琛岀粨鏋溿€傛瘮濡傦細a=1;b=2;c=a+b杩欎笁涓搷浣?鍓嶄袱涓搷浣滃彲浠ラ噸鎺掑簭锛屼絾鏄痗=a+b涓嶄細琚噸鎺掑簭锛屽洜涓鸿淇濊瘉缁撴灉鏄?
馃幆 浣跨敤鍦烘櫙
art
涓婏紝art
鑷甫GC
绾跨▼锛屽啀鍔犱笂App蹇呮湁鐨?strong>涓荤嚎绋嬶紙UI绾跨▼锛夛紝灏辩粍鎴愪簡涓€涓绾跨▼搴旂敤UI 绾跨▼涓轰粈涔堜笉浼氱粨鏉燂紵鍥犱负瀹冨湪鍒濆鍖栧畬姣曞悗浼氭墽饩忔寰幆锛屽惊鐜殑鍐呭鏄埛鏂扮晫饩?/p>
- **骞跺彂鍜屽苟琛屾渶寮€濮嬮兘鏄搷浣滅郴缁熶腑鐨勬蹇碉紝琛ㄧず鐨勬槸CPU鎵ц澶氫釜浠诲姟鐨勬柟寮忋€傝繖涓や釜姒傚康鏋佸鏄撴贩娣嗐€?/li>
- 濡傛灉浣跨敤鐨勬槸鍗曟牳CPU锛岄偅涔堟垜浠槸鏃犳硶鎵ц骞惰鎿嶄綔鐨勶紝鍙兘鍋氬埌骞跺彂锛岃繖鏍锋潵鍏呭垎璋冪敤CPU鐨勮祫婧愩€?/li>
- 濡傛灉鎴戜滑浣跨敤鐨勬槸澶氭牳CPU锛屾垜浠墠鍙互鐪熸鐨勬剰涔変笂鍋氬埌骞惰鎿嶄綔銆?/li>
骞跺彂锛圕oncurrent锛夛紝鍦ㄦ搷浣滅郴缁熶腑锛屾槸鎸囦竴涓椂闂存涓湁鍑犱釜绋嬪簭閮藉浜庡凡鍚姩杩愯鍒拌繍琛屽畬姣曚箣闂达紝涓旇繖鍑犱釜绋嬪簭閮芥槸鍦ㄥ悓涓€涓鐞嗘満涓婅繍琛屻€?/strong>鎿嶄綔绯荤粺鐨勬椂闂寸墖鍒嗘椂璋冨害銆傛墦娓告垙鍜屽惉闊充箰涓や欢浜嬫儏鍦?strong>鍚屼竴涓椂闂存鍐?/strong>閮芥槸鍦ㄥ悓涓€鍙扮數鑴戜笂瀹屾垚浜?strong>浠庡紑濮嬪埌缁撴潫鐨勫姩浣?/strong>銆傞偅涔堬紝灏卞彲浠ヨ鍚煶涔愬拰鎵撴父鎴忔槸骞跺彂鐨勩€?/p>
骞惰锛圥arallel锛夛紝褰撶郴缁烠Pu鏈変竴涓互涓婄殑鍐呮牳鏃讹紝褰撲竴涓唴鏍告墽琛屼竴涓繘绋嬫椂锛屽彟涓€涓唴鏍稿彲浠ユ墽琛屽彟涓€涓繘绋嬶紝涓や釜杩涚▼浜掍笉鎶㈠崰鍐呮牳璧勬簮锛屽彲浠ュ悓鏃惰繘琛岋紝杩欑鏂瑰紡鎴戜滑绉颁箣涓哄苟琛?Parallel)銆?/strong>
婕旂ず鍦ㄥ悓涓€涓▼搴忎腑锛屽苟鍙戣窡骞惰鍙戠敓鐨勫満鏅?/p>
public class TestDemo {
public static void main(String[] args) {
//骞跺彂锛堟姠鍗犲叡鏈夎祫婧愶級
TestThread testThread = new TestThread();
Thread thread = new Thread(testThread, "thread-01");
Thread thread1 = new Thread(testThread, "thread-02");
Thread thread2 = new Thread(testThread, "thread-03");
Thread thread3 = new Thread(testThread, "thread-04");
thread.start();
thread1.start();
thread2.start();
thread3.start();
//骞惰(浜掔浉涓嶆姠鍗犺祫婧?
TestThread testThread1 = new TestThread();
TestThread testThread2 = new TestThread();
testThread1.start();
testThread2.start();
}
static class TestThread extends Thread {
public TestThread() { super("TestThread"); }
private int count = 10;
@Override
public void run() {
super.run();
System.out.println(count--);
}
}
}
馃憮 寮傛鍜屽悓姝?/h2>
鍚屾鍜屽紓姝ュ叧娉ㄧ殑鏄秷鎭€氫俊鏈哄埗.
鍚屾鏄寚: 鍙戦€佹柟鍙戝嚭鏁版嵁鍚? 绛夊緟鎺ユ敹鏂瑰彂鍥炲搷搴斿悗鎵嶅彂涓嬩竴涓暟鎹寘鐨勯€氳鏂瑰紡. 灏辨槸鍦ㄥ彂鍑轰竴涓皟鐢ㄦ椂, 鍦ㄦ病鏈夊緱鍒扮粨鏋滀箣鍓? 璇ヨ皟鐢ㄥ氨涓嶈繑鍥? 浣嗘槸涓€鏃﹁皟鐢ㄨ繑鍥? 灏卞緱鍒拌繑鍥炲€间簡. 涔熷氨鏄敱"璋冪敤鑰?涓诲姩绛夊緟杩欎釜"璋冪敤"鐨勭粨鏋?
寮傛鏄寚: 鍙戦€佹柟鍙戝嚭鏁版嵁鍚? 涓嶇瓑寰呮帴鏀舵柟鍙戝洖鍝嶅簲, 鎺ョ潃鍙戦€佷笅涓暟鎹寘鐨勯€氳鏂瑰紡. 褰撲竴涓紓姝ヨ繃绋嬭皟鐢ㄥ彂鍑哄悗, 璋冪敤鑰呬笉浼氱珛鍒诲緱鍒扮粨鏋? 鑰屾槸鍦ㄨ皟鐢ㄥ彂鍑哄悗, "琚皟鐢ㄨ€?閫氳繃鐘舵€併€侀€氱煡鏉ラ€氱煡璋冪敤鑰? 鎴栭€氳繃鍥炶皟鍑芥暟澶勭悊杩欎釜璋冪敤.
馃憮 浣跨敤绾跨▼鐨勫嚑绉嶆柟寮?/h2>
馃憮 Java
馃幆 Thread
Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
System.out.println("鐩存帴new鍑烘潵锛岀畝鍗曠矖鏆?);
}
};
thread1.start();
馃幆 Thread+Runnable
Runnable target = new Runnable() {
@Override
public void run() {
System.out.println("瀹炰緥涓€涓猺unnable瀵硅薄锛屼氦鐢辩嚎绋嬩娇鐢紝鏂逛究澶嶇敤");
}
};
Thread thread = new Thread(target);
Thread thread2 = new Thread(target);
thread.start();
thread2.start();
馃憮 ThreadFactory+Runnable
ThreadFactory threadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
return new Thread(runnable, "Thread-" + new Random().nextInt(1000));
}
};
Runnable target1 = new Runnable() {
@Override
public void run() {
System.out.println("瀹炰緥涓€涓猺unnable瀵硅薄锛屼氦鐢辩嚎绋嬪伐鍘備娇鐢紝寰楀埌thread瀵硅薄锛屾柟渚垮鐢?);
}
};
threadFactory.newThread(target1);
馃幆 ExecutorService+Runnable
Executor executor = Executors.newSingleThreadExecutor();
Executor executor1 = Executors.newCachedThreadPool();
Executor executor2 = Executors.newFixedThreadPool(10);
Executor executor3 = Executors.newScheduledThreadPool(1);
Runnable target2 = new Runnable() {
@Override
public void run() {
System.out.println("瀹炰緥涓€涓猺unnable瀵硅薄锛屼氦鐢辩嚎绋嬫睜浣跨敤锛岀嚎绋嬫睜甯姪闆嗕腑绠$悊绾跨▼锛岄伩鍏嶈祫婧愭氮璐癸紝鏂逛究澶嶇敤锛?);
}
};
executor.execute(target2);
executor1.execute(target2);
executor2.execute(target2);
executor3.execute(target2);
Future+Callable
Callable<String> callable = new Callable<String>() {
@Override
public String call() {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "鎻愪氦涓€涓换鍔″埌绾跨▼姹犻噷闈㈠幓!";
}
};
ExecutorService executor4 = Executors.newCachedThreadPool();
Future<String> future = executor4.submit(callable);
try {
String result = future.get();
System.out.println("result: " + result);
} catch (InterruptedException | ExecutionException e)
{
e.printStackTrace();
}
馃憮 android涓撳睘
- Handler
- IntentService
- HandlerThread
- AsyncTask
- Rxjava
鑰冭檻鍒扮瘒骞呬互鍙婅繖浜涘紓姝ユ満鍒堕兘鏄ぇ瀹舵瘮杈冪啛鐭ョ殑锛屾殏涓嶅垪鍑轰娇鐢ㄦ柟寮忥紝璐翠笂澶т浆浠啓杩囩殑鏂囩珷閾炬帴锛屼緵澶у浜嗚В锛屽鏋滃悗缁湁闇€瑕佸湪杩涜鍒楀嚭锛屼笉鐒剁瘒骞呭お闀匡紝鐪嬬潃鐪嬬潃寰堝鏄撴伓蹇?/p>
馃憮 kotlin
- Coroutine
馃憮 绾跨▼瀹夊叏
馃憮 涓轰粈涔堜細鍑虹幇绾跨▼瀹夊叏闂
- Java 鍐呭瓨妯″瀷瑙勫畾浜嗘墍鏈夌殑鍙橀噺閮藉瓨鍌ㄥ湪涓诲唴瀛樹腑锛屾瘡鏉$嚎绋嬫湁鑷繁鐨勫伐浣滃唴瀛樸€?/li>
- 绾跨▼鐨勫伐浣滃唴瀛樹腑淇濆瓨浜嗚绾跨▼涓敤鍒扮殑鍙橀噺鐨勪富鍐呭瓨鍓湰鎷疯礉锛岀嚎绋嬪鍙橀噺鐨勬墍鏈夋搷浣滈兘蹇呴』鍦ㄥ伐浣滃唴瀛樹腑杩涜锛岃€屼笉鑳界洿鎺ヨ鍐欎富鍐呭瓨銆?/li>
- 绾跨▼璁块棶涓€涓彉閲忥紝棣栧厛灏嗗彉閲忎粠涓诲唴瀛樻嫹璐濆埌宸ヤ綔鍐呭瓨锛屽鍙橀噺鐨勫啓鎿嶄綔锛屼笉浼氶┈涓婂悓姝ュ埌涓诲唴瀛樸€?/li>
- 涓嶅悓鐨勭嚎绋嬩箣闂翠篃鏃犳硶鐩存帴璁块棶瀵规柟宸ヤ綔鍐呭瓨涓殑鍙橀噺锛岀嚎绋嬮棿鍙橀噺鐨勪紶閫掑潎闇€瑕佽嚜宸辩殑宸ヤ綔鍐呭瓨鍜屼富瀛樹箣闂磋繘琛屾暟鎹悓姝ヨ繘琛屻€?/li>
馃幆 瑙e喅鏂瑰紡
瑙e喅鏂瑰紡閮ㄥ垎鎽樻妱鑷潰璇曞畼锛氳璇村绾跨▼骞跺彂闂
鏈汉瀵逛簬澶氱嚎绋嬬浉鐭ヤ笉澶氾紝鐩墠涔熷湪瀛︿範涓紝璇ラ儴鍒嗘殏鏃跺紩鐢紝绛夊悗缁細鍔犲叆鑷繁鐨勮瑙h繘琛屾敼鍐?/p>
- 淇濊瘉鍏变韩璧勬簮鍦ㄥ悓涓€鏃堕棿鍙兘鐢变竴涓嚎绋嬭繘琛屾搷浣?鍘熷瓙鎬э紝鏈夊簭鎬?銆?/li>
- 灏嗙嚎绋嬫搷浣滅殑缁撴灉鍙婃椂鍒锋柊锛屼繚璇佸叾浠栫嚎绋嬪彲浠ョ珛鍗宠幏鍙栧埌淇敼鍚庣殑鏈€鏂版暟鎹紙鍙鎬э級銆?/li>
馃幆 volatile
- 淇濊瘉鍙鎬э紝涓嶄繚璇佸師瀛愭€?/li>
- 褰撳啓涓€涓?code>volatile鍙橀噺鏃讹紝
JVM
浼氭妸鏈湴鍐呭瓨鐨勫彉閲忓己鍒跺埛鏂板埌涓诲唴瀛樹腑
- 杩欎釜鍐欐搷浣滃鑷村叾浠栫嚎绋嬩腑鐨勭紦瀛樻棤鏁堬紝鍏朵粬绾跨▼璇伙紝浼氫粠涓诲唴瀛樿銆?code>volatile鐨勫啓鎿嶄綔瀵瑰叾瀹冪嚎绋嬪疄鏃跺彲瑙併€?/li>
- 绂佹鎸囦护閲嶆帓搴?/li>
- 涓嶄細瀵瑰瓨鍦ㄤ緷璧栧叧绯荤殑鎸囦护閲嶆帓搴忥紝渚嬪 a = 1;b = a; a 鍜宐瀛樺湪渚濊禆鍏崇郴锛屼笉浼氳閲嶆帓搴?/li>
- 涓嶈兘褰卞搷鍗曠嚎绋嬩笅鐨勬墽琛岀粨鏋溿€傛瘮濡傦細a=1;b=2;c=a+b杩欎笁涓搷浣?鍓嶄袱涓搷浣滃彲浠ラ噸鎺掑簭锛屼絾鏄痗=a+b涓嶄細琚噸鎺掑簭锛屽洜涓鸿淇濊瘉缁撴灉鏄?
馃幆 浣跨敤鍦烘櫙
鍚屾鍜屽紓姝ュ叧娉ㄧ殑鏄秷鎭€氫俊鏈哄埗.
鍚屾鏄寚: 鍙戦€佹柟鍙戝嚭鏁版嵁鍚? 绛夊緟鎺ユ敹鏂瑰彂鍥炲搷搴斿悗鎵嶅彂涓嬩竴涓暟鎹寘鐨勯€氳鏂瑰紡. 灏辨槸鍦ㄥ彂鍑轰竴涓皟鐢ㄦ椂, 鍦ㄦ病鏈夊緱鍒扮粨鏋滀箣鍓? 璇ヨ皟鐢ㄥ氨涓嶈繑鍥? 浣嗘槸涓€鏃﹁皟鐢ㄨ繑鍥? 灏卞緱鍒拌繑鍥炲€间簡. 涔熷氨鏄敱"璋冪敤鑰?涓诲姩绛夊緟杩欎釜"璋冪敤"鐨勭粨鏋?
寮傛鏄寚: 鍙戦€佹柟鍙戝嚭鏁版嵁鍚? 涓嶇瓑寰呮帴鏀舵柟鍙戝洖鍝嶅簲, 鎺ョ潃鍙戦€佷笅涓暟鎹寘鐨勯€氳鏂瑰紡. 褰撲竴涓紓姝ヨ繃绋嬭皟鐢ㄥ彂鍑哄悗, 璋冪敤鑰呬笉浼氱珛鍒诲緱鍒扮粨鏋? 鑰屾槸鍦ㄨ皟鐢ㄥ彂鍑哄悗, "琚皟鐢ㄨ€?閫氳繃鐘舵€併€侀€氱煡鏉ラ€氱煡璋冪敤鑰? 鎴栭€氳繃鍥炶皟鍑芥暟澶勭悊杩欎釜璋冪敤.
馃憮 Java
馃幆 Thread
Thread thread1 = new Thread() {
@Override
public void run() {
super.run();
System.out.println("鐩存帴new鍑烘潵锛岀畝鍗曠矖鏆?);
}
};
thread1.start();
馃幆 Thread+Runnable
Runnable target = new Runnable() {
@Override
public void run() {
System.out.println("瀹炰緥涓€涓猺unnable瀵硅薄锛屼氦鐢辩嚎绋嬩娇鐢紝鏂逛究澶嶇敤");
}
};
Thread thread = new Thread(target);
Thread thread2 = new Thread(target);
thread.start();
thread2.start();
馃憮 ThreadFactory+Runnable
ThreadFactory threadFactory = new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
return new Thread(runnable, "Thread-" + new Random().nextInt(1000));
}
};
Runnable target1 = new Runnable() {
@Override
public void run() {
System.out.println("瀹炰緥涓€涓猺unnable瀵硅薄锛屼氦鐢辩嚎绋嬪伐鍘備娇鐢紝寰楀埌thread瀵硅薄锛屾柟渚垮鐢?);
}
};
threadFactory.newThread(target1);
馃幆 ExecutorService+Runnable
Executor executor = Executors.newSingleThreadExecutor();
Executor executor1 = Executors.newCachedThreadPool();
Executor executor2 = Executors.newFixedThreadPool(10);
Executor executor3 = Executors.newScheduledThreadPool(1);
Runnable target2 = new Runnable() {
@Override
public void run() {
System.out.println("瀹炰緥涓€涓猺unnable瀵硅薄锛屼氦鐢辩嚎绋嬫睜浣跨敤锛岀嚎绋嬫睜甯姪闆嗕腑绠$悊绾跨▼锛岄伩鍏嶈祫婧愭氮璐癸紝鏂逛究澶嶇敤锛?);
}
};
executor.execute(target2);
executor1.execute(target2);
executor2.execute(target2);
executor3.execute(target2);
Future+Callable
Callable<String> callable = new Callable<String>() {
@Override
public String call() {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "鎻愪氦涓€涓换鍔″埌绾跨▼姹犻噷闈㈠幓!";
}
};
ExecutorService executor4 = Executors.newCachedThreadPool();
Future<String> future = executor4.submit(callable);
try {
String result = future.get();
System.out.println("result: " + result);
} catch (InterruptedException | ExecutionException e)
{
e.printStackTrace();
}
馃憮 android涓撳睘
- Handler
- IntentService
- HandlerThread
- AsyncTask
- Rxjava
鑰冭檻鍒扮瘒骞呬互鍙婅繖浜涘紓姝ユ満鍒堕兘鏄ぇ瀹舵瘮杈冪啛鐭ョ殑锛屾殏涓嶅垪鍑轰娇鐢ㄦ柟寮忥紝璐翠笂澶т浆浠啓杩囩殑鏂囩珷閾炬帴锛屼緵澶у浜嗚В锛屽鏋滃悗缁湁闇€瑕佸湪杩涜鍒楀嚭锛屼笉鐒剁瘒骞呭お闀匡紝鐪嬬潃鐪嬬潃寰堝鏄撴伓蹇?/p>
馃憮 kotlin
- Coroutine
馃憮 绾跨▼瀹夊叏
馃憮 涓轰粈涔堜細鍑虹幇绾跨▼瀹夊叏闂
- Java 鍐呭瓨妯″瀷瑙勫畾浜嗘墍鏈夌殑鍙橀噺閮藉瓨鍌ㄥ湪涓诲唴瀛樹腑锛屾瘡鏉$嚎绋嬫湁鑷繁鐨勫伐浣滃唴瀛樸€?/li>
- 绾跨▼鐨勫伐浣滃唴瀛樹腑淇濆瓨浜嗚绾跨▼涓敤鍒扮殑鍙橀噺鐨勪富鍐呭瓨鍓湰鎷疯礉锛岀嚎绋嬪鍙橀噺鐨勬墍鏈夋搷浣滈兘蹇呴』鍦ㄥ伐浣滃唴瀛樹腑杩涜锛岃€屼笉鑳界洿鎺ヨ鍐欎富鍐呭瓨銆?/li>
- 绾跨▼璁块棶涓€涓彉閲忥紝棣栧厛灏嗗彉閲忎粠涓诲唴瀛樻嫹璐濆埌宸ヤ綔鍐呭瓨锛屽鍙橀噺鐨勫啓鎿嶄綔锛屼笉浼氶┈涓婂悓姝ュ埌涓诲唴瀛樸€?/li>
- 涓嶅悓鐨勭嚎绋嬩箣闂翠篃鏃犳硶鐩存帴璁块棶瀵规柟宸ヤ綔鍐呭瓨涓殑鍙橀噺锛岀嚎绋嬮棿鍙橀噺鐨勪紶閫掑潎闇€瑕佽嚜宸辩殑宸ヤ綔鍐呭瓨鍜屼富瀛樹箣闂磋繘琛屾暟鎹悓姝ヨ繘琛屻€?/li>
馃幆 瑙e喅鏂瑰紡
瑙e喅鏂瑰紡閮ㄥ垎鎽樻妱鑷潰璇曞畼锛氳璇村绾跨▼骞跺彂闂
鏈汉瀵逛簬澶氱嚎绋嬬浉鐭ヤ笉澶氾紝鐩墠涔熷湪瀛︿範涓紝璇ラ儴鍒嗘殏鏃跺紩鐢紝绛夊悗缁細鍔犲叆鑷繁鐨勮瑙h繘琛屾敼鍐?/p>
- 淇濊瘉鍏变韩璧勬簮鍦ㄥ悓涓€鏃堕棿鍙兘鐢变竴涓嚎绋嬭繘琛屾搷浣?鍘熷瓙鎬э紝鏈夊簭鎬?銆?/li>
- 灏嗙嚎绋嬫搷浣滅殑缁撴灉鍙婃椂鍒锋柊锛屼繚璇佸叾浠栫嚎绋嬪彲浠ョ珛鍗宠幏鍙栧埌淇敼鍚庣殑鏈€鏂版暟鎹紙鍙鎬э級銆?/li>
馃幆 volatile
- 淇濊瘉鍙鎬э紝涓嶄繚璇佸師瀛愭€?/li>
- 褰撳啓涓€涓?code>volatile鍙橀噺鏃讹紝
JVM
浼氭妸鏈湴鍐呭瓨鐨勫彉閲忓己鍒跺埛鏂板埌涓诲唴瀛樹腑- 杩欎釜鍐欐搷浣滃鑷村叾浠栫嚎绋嬩腑鐨勭紦瀛樻棤鏁堬紝鍏朵粬绾跨▼璇伙紝浼氫粠涓诲唴瀛樿銆?code>volatile鐨勫啓鎿嶄綔瀵瑰叾瀹冪嚎绋嬪疄鏃跺彲瑙併€?/li>
- 绂佹鎸囦护閲嶆帓搴?/li>
- 涓嶄細瀵瑰瓨鍦ㄤ緷璧栧叧绯荤殑鎸囦护閲嶆帓搴忥紝渚嬪 a = 1;b = a; a 鍜宐瀛樺湪渚濊禆鍏崇郴锛屼笉浼氳閲嶆帓搴?/li>
- 涓嶈兘褰卞搷鍗曠嚎绋嬩笅鐨勬墽琛岀粨鏋溿€傛瘮濡傦細a=1;b=2;c=a+b杩欎笁涓搷浣?鍓嶄袱涓搷浣滃彲浠ラ噸鎺掑簭锛屼絾鏄痗=a+b涓嶄細琚噸鎺掑簭锛屽洜涓鸿淇濊瘉缁撴灉鏄?
馃幆 浣跨敤鍦烘櫙
瀵逛簬涓€涓彉閲忥紝鍙湁涓€涓嚎绋嬫墽琛屽啓鎿嶄綔锛屽叾瀹冪嚎绋嬮兘鏄鎿嶄綔锛岃繖鏃跺€欏彲浠ョ敤 volatile
淇グ杩欎釜鍙橀噺銆?/p>
馃幆 鍗曚緥鍙岄噸閿佷负浠€涔堣鐢ㄥ埌volatile锛?/h4>
public class TestInstance {
private static volatile TestInstance mInstance;
public static TestInstance getInstance(){ //1
if (mInstance == null){ //2
synchronized (TestInstance.class){ //3
if (mInstance == null){ //4
mInstance = new TestInstance(); //5
}
}
}
return mInstance;
}
public class TestInstance {
private static volatile TestInstance mInstance;
public static TestInstance getInstance(){ //1
if (mInstance == null){ //2
synchronized (TestInstance.class){ //3
if (mInstance == null){ //4
mInstance = new TestInstance(); //5
}
}
}
return mInstance;
}
鍋囧娌℃湁鐢?code>volatile锛屽苟鍙戞儏鍐典笅浼氬嚭鐜伴棶棰橈紝绾跨▼A鎵ц鍒版敞閲? new TestInstance()
鐨勬椂鍊欙紝鍒嗕负濡備笅鍑犱釜鍑犳鎿嶄綔锛?/p>
- 1銆佸垎閰嶅唴瀛?/li>
- 2銆佸垵濮嬪寲瀵硅薄
- 3銆?code>mInstance 鎸囧悜鍐呭瓨
杩欐椂鍊欏鏋滃彂鐢熸寚浠ら噸鎺掞紝鎵ц椤哄簭鏄?32锛屾墽琛屽埌绗?鐨勬椂鍊欙紝绾跨▼B鍒氬ソ杩涙潵浜嗭紝骞朵笖鎵ц鍒版敞閲?锛岃繖鏃跺€欏垽鏂?code>mInstance涓嶄负绌猴紝鐩存帴浣跨敤涓€涓湭鍒濆鍖栫殑瀵硅薄銆傛墍浠ヤ娇鐢?code>volatile鍏抽敭瀛楁潵绂佹鎸囦护閲嶆帓搴忋€?/p>
馃憮 volatile 鍘熺悊
鍦?code>JVM搴曞眰volatile
鏄噰鐢?strong>鍐呭瓨灞忛殰鏉ュ疄鐜扮殑锛屽唴瀛樺睆闅滀細鎻愪緵3涓姛鑳斤細
- 瀹冪‘淇濇寚浠ら噸鎺掑簭鏃朵笉浼氭妸鍏跺悗闈㈢殑鎸囦护鎺掑埌鍐呭瓨灞忛殰涔嬪墠鐨勪綅缃紝涔熶笉浼氭妸鍓嶉潰鐨勬寚浠ゆ帓鍒板唴瀛樺睆闅滅殑鍚庨潰锛涘嵆鍦ㄦ墽琛屽埌鍐呭瓨灞忛殰杩欏彞鎸囦护鏃讹紝鍦ㄥ畠鍓嶉潰鐨勬搷浣滃凡缁忓叏閮ㄥ畬鎴愶紱
- 瀹冧細寮哄埗灏嗙紦瀛樼殑淇敼鎿嶄綔绔嬪嵆鍐欏埌涓诲唴瀛?/li>
- 鍐欐搷浣滀細瀵艰嚧鍏跺畠CPU涓殑缂撳瓨琛屽け鏁堬紝鍐欎箣鍚庯紝鍏跺畠绾跨▼鐨勮鎿嶄綔浼氫粠涓诲唴瀛樿銆?/li>
馃憮 volatile 鐨勫眬闄愭€?/h4>
volatile 鍙兘淇濊瘉鍙鎬э紝涓嶈兘淇濊瘉鍘熷瓙鎬?/strong>鍐欐搷浣滃鍏跺畠绾跨▼鍙锛屼絾鏄笉鑳借В鍐冲涓嚎绋嬪悓鏃跺啓鐨勯棶棰樸€?/p>
澶氫釜绾跨▼鍚屾椂鍐欎竴涓彉閲忋€?/strong> 渚嬪鍞エ锛屼綑绁ㄦ槸100寮狅紝绐楀彛A鍜岀獥鍙鍚屾椂鍚勫崠鍑轰竴寮犵エ锛?鍋囧浣欑エ鍙橀噺鐢? 鍓嶉潰璇村埌馃憮 Synchronized
馃憮 浣跨敤鍦烘櫙
volatile
淇グ锛屾槸鏈夐棶棰樼殑銆?br>
A绐楀彛鑾峰彇浣欑エ鏄?00锛孊绐楀彛鑾峰彇浣欑エ涔熸槸100锛孉鍗栧嚭涓€寮犲彉鎴?9锛屽埛鏂板洖涓诲唴瀛橈紝鍚屾椂B鍗栧嚭涓€寮犲彉鎴?9锛屼篃鍒锋柊鍥炰富鍐呭瓨锛屼細瀵艰嚧鏈€缁堜富鍐呭瓨浣欑エ鏄?9鑰屼笉鏄?8銆?/p>
volatile
鐨勫眬闄愭€э紝灏辨槸澶氫釜绾跨▼鍚屾椂鍐欑殑鎯呭喌锛岃繖绉嶆儏鍐典竴鑸彲浠ヤ娇鐢?strong>Synchronized
Synchronized 鍙互淇濊瘉鍚屼竴鏃跺埢锛屽彧鏈変竴涓嚎绋嬪彲鎵ц鏌愪釜鏂规硶鎴栨煇涓唬鐮佸潡銆?/strong>
馃憮 Synchronized 鍘熺悊
public class SynchronizedTest {
public static void main(String[] args) {
synchronized (SynchronizedTest.class) {
System.out.println("123");
}
method();
}
private static void method() {
}
}
灏嗚繖娈典唬鐮佸厛鐢?code>javac鍛戒护缂栬瘧锛屽啀java p -v SynchronizedTest.class
鍛戒护鏌ョ湅瀛楄妭鐮侊紝閮ㄥ垎瀛楄妭鐮佸涓?/p>
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=3, args_size=1
0: ldc #2 // class com/lanshifu/opengldemo/test/SynchronizedTest
2: dup
3: astore_1
4: monitorenter
5: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
8: ldc #4 // String 123
10: invokevirtual #5 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
13: aload_1
14: monitorexit
15: goto 23
18: astore_2
19: aload_1
20: monitorexit
21: aload_2
22: athrow
23: invokestatic #6 // Method method:()V
26: return
鍙互鐪嬪埌 4: monitorenter
鍜?14: monitorexit
锛屼腑闂存槸鎵撳嵃鐨勮鍙ャ€?/p>
鎵ц鍚屾浠g爜鍧楋紝棣栧厛浼氭墽琛?code>monitorenter鎸囦护锛岀劧鍚庢墽琛屽悓姝ヤ唬鐮佸潡涓殑浠g爜锛岄€€鍑哄悓姝ヤ唬鐮佸潡鐨勬椂鍊欎細鎵цmonitorexit
鎸囦护 銆?/p>
浣跨敤
Synchronized
杩涜鍚屾锛屽叾鍏抽敭灏辨槸蹇呴』瑕佸瀵硅薄鐨勭洃瑙嗗櫒monitor
杩涜鑾峰彇锛屽綋绾跨▼鑾峰彇monitor
鍚庢墠鑳界户缁線涓嬫墽琛岋紝鍚﹀垯灏辫繘鍏ュ悓姝ラ槦鍒楋紝绾跨▼鐘舵€佸彉鎴怋LOCK锛屽悓涓€鏃跺埢鍙湁涓€涓嚎绋嬭兘澶熻幏鍙栧埌monitor
锛屽綋鐩戝惉鍒?code>monitorexit琚皟鐢紝闃熷垪閲屽氨鏈変竴涓嚎绋嬪嚭闃燂紝鑾峰彇monitor
銆傝鎯呭弬鑰冿細www.jianshu.com/p/d53bf830f鈥?/p>
姣忎釜瀵硅薄鎷ユ湁涓€涓鏁板櫒锛屽綋绾跨▼鑾峰彇璇ュ璞¢攣鍚庯紝璁℃暟鍣ㄥ氨浼氬姞涓€锛岄噴鏀鹃攣鍚庡氨浼氬皢璁℃暟鍣ㄥ噺涓€锛屾墍浠ュ彧瑕佽繖涓攣鐨勮鏁板櫒澶т簬0锛屽叾瀹冪嚎绋嬭闂氨鍙兘绛夊緟銆?/p>
馃憮 Synchronized閿佺殑鍗囩骇
澶у瀵?code>Synchronized鐨勭悊瑙e彲鑳藉氨鏄噸閲忕骇閿侊紝浣嗘槸Java1.6
瀵?Synchronized
杩涜浜嗗悇绉嶄紭鍖栦箣鍚庯紝鏈変簺鎯呭喌涓嬪畠灏卞苟涓嶉偅涔堥噸锛孞ava1.6 涓负浜嗗噺灏戣幏寰楅攣鍜岄噴鏀鹃攣甯︽潵鐨勬€ц兘娑堣€楄€屽紩鍏ョ殑鍋忓悜閿?/strong>鍜?strong>杞婚噺绾ч攣銆?/p>
鍋忓悜閿侊細 澶у鏁版儏鍐典笅锛岄攣涓嶄粎涓嶅瓨鍦ㄥ绾跨▼绔炰簤锛岃€屼笖鎬绘槸鐢卞悓涓€绾跨▼澶氭鑾峰緱锛屼负浜嗚绾跨▼鑾峰緱閿佺殑浠d环鏇翠綆鑰屽紩鍏ヤ簡鍋忓悜閿併€?/p>
褰撲竴涓嚎绋婣璁块棶鍔犱簡鍚屾閿佺殑浠g爜鍧楁椂锛屼細鍦ㄥ璞″ご涓瓨 鍌ㄥ綋鍓嶇嚎绋嬬殑id锛屽悗缁繖涓嚎绋嬭繘鍏ュ拰閫€鍑鸿繖娈靛姞浜嗗悓姝ラ攣鐨勪唬鐮佸潡鏃讹紝涓嶉渶瑕佸啀娆″姞閿佸拰閲婃斁閿併€?/p>
杞婚噺绾ч攣锛?/strong> 鍦ㄥ亸鍚戦攣鎯呭喌涓嬶紝濡傛灉绾跨▼B涔熻闂簡鍚屾浠g爜鍧楋紝姣旇緝瀵硅薄澶寸殑绾跨▼id涓嶄竴鏍凤紝浼氬崌绾т负杞婚噺绾ч攣锛屽苟涓旈€氳繃鑷棆鐨勬柟寮忔潵鑾峰彇杞婚噺绾ч攣銆?/p>
閲嶉噺绾ч攣锛?/strong> 濡傛灉绾跨▼A鍜岀嚎绋婤鍚屾椂璁块棶鍚屾浠g爜鍧楋紝鍒欒交閲忕骇閿佷細鍗囩骇涓洪噸閲忕骇閿侊紝绾跨▼A鑾峰彇鍒伴噸閲忕骇閿佺殑鎯呭喌涓嬶紝绾跨▼B鍙兘鍏ラ槦绛夊緟锛岃繘鍏LOCK鐘舵€併€?/p>
涓婇潰璇村埌 鍦ㄥ涓潯浠跺彉閲忓拰楂樺害绔炰簤閿佺殑鍦版柟锛岀敤ReentrantLock鏇村悎閫?/strong>锛?code>ReentrantLock杩樻彁渚涗簡 lock 鍜?unlock 瀹炵幇鍙畾鏃剁殑閿佽姹傦細tryLock 鎵撳嵃鐨勬棩蹇楋細 涓婇潰婕旂ず浜?code>trtLock鐨勪娇鐢紝 杩愯鎵撳嵃鏃ュ織 涓婇潰婕旂ず浜?code>Condition鐨?await 鍜?signal 浣跨敤锛屽墠鎻愯鍏坙ock銆?/p>
鍏钩閿佽〃绀虹嚎绋嬭幏鍙栭攣鐨勯『搴忔槸鎸夌収绾跨▼鍔犻攣鐨勯『搴忔潵鍒嗛厤鐨勶紝鍗冲厛鏉ュ厛寰楃殑椤哄簭銆傝€岄潪鍏钩閿佸氨鏄竴绉嶉攣鐨勬姠鍗犳満鍒讹紝鏄殢鏈鸿幏寰楅攣鐨勶紝鍙兘浼氬鑷存煇浜涚嚎绋嬩竴鑷存嬁涓嶅埌閿侊紝鎵€浠ユ槸涓嶅叕骞崇殑銆?/p>
閫氳繃涓婇潰鍒嗘瀽锛屽苟鍙戜弗閲嶇殑鎯呭喌涓嬶紝浣跨敤閿佹樉鐒舵晥鐜囦綆涓嬶紝鍥犱负鍚屼竴鏃跺埢鍙兘鏈変竴涓嚎绋嬪彲浠ヨ幏寰楅攣锛屽叾瀹冪嚎绋嬪彧鑳戒箹涔栫瓑寰呫€?/p>
Java鎻愪緵浜嗗苟鍙戝寘瑙e喅杩欎釜闂锛屾帴涓嬫潵浠嬬粛骞跺彂鍖呴噷涓€浜涘父鐢ㄧ殑鏁版嵁缁撴瀯銆?/p>
鎴戜滑閮界煡閬揌ashMap鏄嚎绋嬩笉瀹夊叏鐨勬暟鎹粨鏋勶紝 鎵€浠ョ悊璁轰笂鏈€澶氭敮鎸?6涓嚎绋嬪悓鏃惰闂€?/p>
閾捐〃缁撴瀯鐨勯樆濉為槦鍒楋紝鍐呴儴浣跨敤澶氫釜 婧愮爜涓嶈创澶锛岀畝鍗曡涓€涓?code>LinkBlockingQueue 鐨勯€昏緫锛?/p>
鍐呴儴閲囩敤 涓句竴涓?code>int 婧愮爜鐪嬩竴涓?/p>
U 鏄? 閫氳繃 璇ラ儴鍒嗘憳鎶勮嚜java涓殑澶氱嚎绋嬶細绾跨▼浣跨敤銆佺嚎绋嬪畨鍏ㄣ€佺嚎绋嬮€氫俊 杩欑鏂瑰紡涓昏鏄鍏ㄥ眬鍙橀噺鍔犻攣锛屽嵆鐢?code>synchronized鍏抽敭瀛楀瀵硅薄鎴栬€呬唬鐮佸潡鍔犻攣 杩欑鏂瑰紡鍙瑙佷笂涓€鑺傜嚎绋嬪悓姝ヤ腑鐨勪緥瀛愩€?/p>
鍩轰簬鈥滈攣鈥濈殑鏂瑰紡闇€瑕佺嚎绋嬩笉鏂幓灏濊瘯鑾峰緱閿侊紝杩欎細鑰楄垂鏈嶅姟鍣ㄨ祫婧愩€?/p>
Java澶氱嚎绋嬬殑绛夊緟/閫氱煡鏈哄埗鏄熀浜?code>Object绫荤殑 褰撲富绾跨▼鍒涘缓骞跺惎鍔ㄤ簡鑰楁椂瀛愮嚎绋嬶紝鑰屼富绾跨▼鏃╀簬瀛愮嚎绋嬬粨鏉熶箣鍓嶇粨鏉熸椂锛屽氨鍙互鐢?code>join鏂规硶绛夊瓙绾跨▼鎵ц瀹屾瘯鍚庯紝浠庤€岃涓荤嚎绋嬭幏寰楀瓙绾跨▼涓殑澶勭悊瀹岀殑鏌愪釜鏁版嵁銆?/p>
杩欓噷闇€瑕佸己璋冧竴涓嬶細sleep鏂规硶鏄笉浼氶噴鏀惧綋鍓嶇殑閿佺殑锛岃€寃ait鏂规硶浼?/strong>銆傝繖涔熸槸鏈€甯歌鐨勪竴涓绾跨▼闈㈣瘯棰樸€?/p>
鍙互鐪嬪埌锛?code>ThreadA鍙互瀛樺彇鑷繁褰撳墠绾跨▼鐨勪竴涓€笺€傚鏋滃紑鍙戣€呭笇鏈涘皢绫荤殑鏌愪釜闈欐€佸彉閲?code>锛坲ser ID鎴栬€卼ransaction ID锛?/code>涓庣嚎绋嬬姸鎬佸叧鑱旓紝鍒欏彲浠ヨ€冭檻浣跨敤 JDK鎻愪緵浜嗕竴涓被浼间簬鈥滀俊鍙烽噺鈥濆姛鑳界殑绫?code>Semaphore銆傚湪澶氫釜绾跨▼锛堣秴杩?涓級闇€瑕佺浉浜掑悎浣滅殑鍦烘櫙涓嬶紝鎴戜滑鐢ㄧ畝鍗曠殑鈥滈攣鈥濆拰鈥滅瓑寰呴€氱煡鏈哄埗鈥濆氨涓嶉偅涔堟柟渚夸簡銆傝繖涓椂鍊欏氨鍙互鐢ㄥ埌淇″彿閲忋€侸DK涓彁渚涚殑寰堝澶氱嚎绋嬮€氫俊宸ュ叿绫婚兘鏄熀浜庝俊鍙烽噺妯″瀷鐨勩€?/p>
绠¢亾鏄熀浜庘€滅閬撴祦鈥濈殑閫氫俊鏂瑰紡銆侸DK鎻愪緵浜?code>PipedWriter銆? 搴旂敤鍦烘櫙锛氱閬撳鍗婁笌I/O娴佺浉鍏炽€傚綋鎴戜滑涓€涓嚎绋嬮渶瑕佸厛鍙︿竴涓嚎绋嬪彂閫佷竴涓俊鎭紙姣斿瀛楃涓诧級鎴栬€呮枃浠剁瓑绛夋椂锛屽氨闇€瑕佷娇鐢ㄧ閬撻€氫俊浜嗐€?/p>
浣滆€咃細BuildF馃憮 Synchronized 缂虹偣
馃憮 ReentrantLock
Synchronized
鐨勭己鐐癸紝涓嶈兘璁剧疆閿佽秴鏃舵椂闂村拰涓嶈兘閫氳繃浠g爜閲婃斁閿侊紝ReentranLock
灏卞彲浠ヨВ鍐宠繖涓棶棰樸€?/p>
Condition
锛屽绾跨▼鐨勭瓑寰呭拰鍞ら啋绛夋搷浣滄洿鍔犵伒娲伙紝涓€涓?code>ReentrantLock鍙互鏈夊涓?code>Condition瀹炰緥锛屾墍浠ユ洿鏈夋墿灞曟€с€?/p>
馃憮 ReentrantLock 鐨勪娇鐢?/h4>
ReentrantLock reentrantLock = new ReentrantLock();
System.out.println("reentrantLock->lock");
reentrantLock.lock();
try {
System.out.println("鐫$湢2绉?..");
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
reentrantLock.unlock();
System.out.println("reentrantLock->unlock");
}
public static void main(String[] args) {
ReentrantLock reentrantLock = new ReentrantLock();
Thread thread1 = new Thread_tryLock(reentrantLock);
thread1.setName("thread1");
thread1.start();
Thread thread2 = new Thread_tryLock(reentrantLock);
thread2.setName("thread2");
thread2.start();
}
static class Thread_tryLock extends Thread {
ReentrantLock reentrantLock;
public Thread_tryLock(ReentrantLock reentrantLock) {
this.reentrantLock = reentrantLock;
}
@Override
public void run() {
try {
System.out.println("try lock:" + Thread.currentThread().getName());
boolean tryLock = reentrantLock.tryLock(3, TimeUnit.SECONDS);
if (tryLock) {
System.out.println("try lock success :" + Thread.currentThread().getName());
System.out.println("鐫$湢涓€涓嬶細" + Thread.currentThread().getName());
Thread.sleep(5000);
System.out.println("閱掍簡锛? + Thread.currentThread().getName());
} else {
System.out.println("try lock 瓒呮椂 :" + Thread.currentThread().getName());
}
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("unlock:" + Thread.currentThread().getName());
reentrantLock.unlock();
}
}
}
try lock:thread1
try lock:thread2
try lock success :thread2
鐫$湢涓€涓嬶細thread2
try lock 瓒呮椂 :thread1
unlock:thread1
Exception in thread "thread1" java.lang.IllegalMonitorStateException
at java.util.concurrent.locks.ReentrantLock$Sync.tryRelease(ReentrantLock.java:151)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.release(AbstractQueuedSynchronizer.java:1261)
at java.util.concurrent.locks.ReentrantLock.unlock(ReentrantLock.java:457)
at com.lanshifu.demo_module.test.lock.ReentranLockTest$Thread_tryLock.run(ReentranLockTest.java:60)
閱掍簡锛歵hread2
unlock:thread2
trtLock
璁剧疆鑾峰彇閿佺殑绛夊緟鏃堕棿锛岃秴杩?绉掔洿鎺ヨ繑鍥炲け璐ワ紝鍙互浠庢棩蹇椾腑鐪嬪埌缁撴灉銆?鏈夊紓甯告槸鍥犱负thread1
鑾峰彇閿佸け璐ワ紝涓嶅簲璇ヨ皟鐢?code>unlock銆?/p>
馃憮 Condition 鏉′欢
public static void main(String[] args) {
Thread_Condition thread_condition = new Thread_Condition();
thread_condition.setName("娴嬭瘯Condition鐨勭嚎绋?);
thread_condition.start();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
thread_condition.singal();
}
static class Thread_Condition extends Thread {
@Override
public void run() {
await();
}
private ReentrantLock lock = new ReentrantLock();
public Condition condition = lock.newCondition();
public void await() {
try {
System.out.println("lock");
lock.lock();
System.out.println(Thread.currentThread().getName() + ":鎴戝湪绛夊緟閫氱煡鐨勫埌鏉?..");
condition.await();//await 鍜?signal 瀵瑰簲
//condition.await(2, TimeUnit.SECONDS); //璁剧疆绛夊緟瓒呮椂鏃堕棿
System.out.println(Thread.currentThread().getName() + ":绛夊埌閫氱煡浜嗭紝鎴戠户缁墽琛?gt;>>");
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println("unlock");
lock.unlock();
}
}
public void singal() {
try {
System.out.println("lock");
lock.lock();
System.out.println("鎴戣閫氱煡鍦ㄧ瓑寰呯殑绾跨▼锛宑ondition.signal()");
condition.signal();//await 鍜?signal 瀵瑰簲
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("unlock");
lock.unlock();
}
}
}
lock
娴嬭瘯Condition鐨勭嚎绋?鎴戝湪绛夊緟閫氱煡鐨勫埌鏉?..
lock
鎴戣閫氱煡鍦ㄧ瓑寰呯殑绾跨▼锛宑ondition.signal()
unlock
娴嬭瘯Condition鐨勭嚎绋?绛夊埌閫氱煡浜嗭紝鎴戠户缁墽琛?gt;>>
unlock
馃憮 鍏钩閿佷笌闈炲叕骞抽攣
ReentrantLock
鏋勯€犲嚱鏁颁紶true琛ㄧず鍏钩閿併€?/p>
馃憮 ReentrantLock 娉ㄦ剰鐐?/h4>
ReentrantLock
浣跨敤lock
鍜?code>unlock鏉ヨ幏寰楅攣鍜岄噴鏀鹃攣unlock
瑕佹斁鍦?code>finally涓紝杩欐牱姝e父杩愯鎴栬€呭紓甯搁兘浼氶噴鏀鹃攣condition
鐨?code>await鍜?code>signal鏂规硶涔嬪墠锛屽繀椤昏皟鐢?code>lock鏂规硶鑾峰緱瀵硅薄鐩戣鍣?/li>
馃憮 骞跺彂鍖?/h2>
馃憮 ConcurrentHashMap
HashTable
鍒欏湪HashMap
鍩虹涓婏紝get
鏂规硶鍜?code>put鏂规硶鍔犱笂Synchronized
淇グ鍙樻垚绾跨▼瀹夊叏锛屼笉杩囧湪楂樺苟鍙戞儏鍐典笅鏁堢巼搴曚笅锛屾渶缁堣ConcurrentHashMap
鏇夸唬銆?/p>
ConcurrentHashMap
閲囩敤鍒嗘閿侊紝鍐呴儴榛樿鏈?6涓《锛?code>get鍜?code>put鎿嶄綔锛岄鍏堝皢key璁$畻hashcode
锛岀劧鍚庤窡16鍙栦綑锛岃惤鍒?6涓《涓殑涓€涓紝鐒跺悗姣忎釜妗朵腑閮藉姞浜嗛攣锛?code>ReentrantLock锛夛紝妗朵腑鏄?code>HashMap缁撴瀯锛堟暟缁勫姞閾捐〃锛岄摼琛ㄨ繃闀胯浆绾㈤粦鏍戯級銆?/p>
馃憮 LinkBlockingQueue
ReentrantLock
/** Lock held by take, poll, etc */
private final ReentrantLock takeLock = new ReentrantLock();
/** Wait queue for waiting takes */
private final Condition notEmpty = takeLock.newCondition();
/** Lock held by put, offer, etc */
private final ReentrantLock putLock = new ReentrantLock();
/** Wait queue for waiting puts */
private final Condition notFull = putLock.newCondition();
private void signalNotEmpty() {
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
try {
notEmpty.signal();
} finally {
takeLock.unlock();
}
}
/**
* Signals a waiting put. Called only from take/poll.
*/
private void signalNotFull() {
final ReentrantLock putLock = this.putLock;
putLock.lock();
try {
notFull.signal();
} finally {
putLock.unlock();
}
}
notEmpty.signal();
锛岄€氱煡娑堣垂鑰咃紝1涓殑绛夊緟缁撴潫锛屽敜閱掔户缁墽琛屻€?/li>
notFull.signal();
锛岄€氱煡鐢熶骇鑰呯户缁敓浜с€?/li>
notFull.signal();
锛岃繖鏃跺€欑敓浜ц€呮墠鑳界户缁敓浜с€?/li>
LinkBlockingQueue
鏄吀鍨嬬殑鐢熶骇鑰呮秷璐硅€呮ā寮忥紝婧愮爜缁嗚妭灏变笉澶氳銆?/p>
馃憮 鍘熷瓙鎿嶄綔绫伙細AtomicInteger
CAS锛坈ompare and swap锛?/code>淇濊瘉鍘熷瓙鎬?/p>
鑷鐨勪緥瀛?/p>
AtomicInteger atomicInteger = new AtomicInteger(0);
atomicInteger.incrementAndGet();//鑷
/**
* Atomically increments by one the current value.
*
* @return the updated value
*/
public final int incrementAndGet() {
return U.getAndAddInt(this, VALUE, 1) + 1;
}
Unsafe
锛岀湅涓?Unsafe#getAndAddInt
public final int getAndAddInt(Object var1, long var2, int var4) {
int var5;
do {
var5 = this.getIntVolatile(var1, var2);
} while(!this.compareAndSwapInt(var1, var2, var5, var5 + var4));
return var5;
}
compareAndSwapInt
淇濊瘉鍘熷瓙鎬с€?/p>
馃憮 绾跨▼閫氫俊
鏈汉瀵逛簬澶氱嚎绋嬬浉鐭ヤ笉澶氾紝鐩墠涔熷湪瀛︿範涓紝璇ラ儴鍒嗘殏鏃跺紩鐢紝绛夊悗缁細鍔犲叆鑷繁鐨勮瑙h繘琛屾敼鍐?绾跨▼鏈夎嚜宸辩殑绉佹湁绌洪棿锛屼絾褰撴垜澶氫釜绾跨▼涔嬮棿鐩镐簰鍗忎綔鐨勬椂鍊欙紝灏遍渶瑕佽繘琛岀嚎绋嬮棿閫氫俊鏂癸紝鏈妭灏嗕粙缁岼ava绾跨▼涔嬮棿鐨勫嚑绉嶉€氫俊鍘熺悊銆?/p>
馃幆閿佷笌鍚屾
lock
锛屾潵杈炬垚绾跨▼闂撮€氫俊銆?/p>
馃幆绛夊緟/閫氱煡鏈哄埗
wait()
鏂规硶鍜?code>notify(), notifyAll()
鏂规硶鏉ュ疄鐜扮殑锛?/p>
wait()
鏂规硶鍜?code>notify()鏂规硶蹇呴』鍐欏湪synchronized
浠g爜鍧楅噷闈細wait()
鍜?code>notify()鏂规硶蹇呴』閫氳繃鑾峰彇鐨勯攣瀵硅薄杩涜璋冪敤锛屽洜涓?code>wait灏辨槸绾跨▼鍦ㄨ幏鍙栧璞¢攣鍚庯紝涓诲姩閲婃斁瀵硅薄閿侊紝鍚屾椂浼戠湢鏈嚎绋嬶紝鐩村埌鏈夊叾瀹冪嚎绋嬭皟鐢ㄥ璞$殑notify()
鍞ら啋璇ョ嚎绋嬶紝鎵嶈兘缁х画鑾峰彇瀵硅薄閿侊紝骞剁户缁墽琛屻€傜浉搴旂殑notify()
灏辨槸瀵瑰璞¢攣鐨勫敜閱掓搷浣滐紝鍥犺€屽繀椤绘斁鍦ㄥ姞閿佺殑synchronized
浠g爜鍧楃幆澧冨唴銆?/p>
notify()
鏂规硶浼氶殢鏈哄彨閱掍竴涓鍦ㄧ瓑寰呯殑绾跨▼锛岃€?code>notifyAll()浼氬彨閱掓墍鏈夋鍦ㄧ瓑寰呯殑绾跨▼锛岃鍞ら啋鐨勭嚎绋嬮噸鏂板湪灏辩华闃熷垪涓寜鐓т竴瀹氱畻娉曟渶缁堝啀娆¤澶勭悊鏈鸿幏寰楀苟杩涜澶勭悊锛岃€屼笉鏄珛椹噸鏂拌幏寰楀鐞嗘満銆?/p>
public class mythread {
private static Object lock = new Object();
static class ThreadA implements Runnable {
@Override
public void run() {
synchronized (lock) {
for (int i = 0; i < 5; i++) {
try {
System.out.println("ThreadA: " + i);
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.notify();
}
}
}
static class ThreadB implements Runnable {
@Override
public void run() {
synchronized (lock) {
for (int i = 0; i < 5; i++) {
try {
System.out.println("ThreadB: " + i);
lock.notify();
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lock.notify();
}
}
}
public static void main(String[] args) {
new Thread(new ThreadA()).start();
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(new ThreadB()).start();
}
}
馃幆join鏂规硶
join()
鏂规硶璁╁綋鍓嶇嚎绋嬮櫡鍏モ€滅瓑寰呪€濈姸鎬侊紝绛?code>join鐨勮繖涓嚎绋嬫墽琛屽畬鎴愬悗锛屽啀缁х画鎵ц褰撳墠绾跨▼銆?/p>
join()
鏂规硶鍙婂叾閲嶈浇鏂规硶搴曞眰閮芥槸鍒╃敤浜?code>wait(long)杩欎釜鏂规硶銆?/p>
public class mythread {
static class ThreadA implements Runnable {
@Override
public void run() {
try {
System.out.println("瀛愮嚎绋嬬潯涓€绉?);
Thread.sleep(1000);
System.out.println("瀛愮嚎绋嬬潯瀹屼簡涓€绉?);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new ThreadA());
thread.start();
thread.join();
System.out.println("濡傛灉涓嶅姞join鏂规硶锛岃繖琛屽氨浼氬厛鎵撳嵃鍑烘潵");
}
}
馃幆sleep鏂规硶
sleep
鏂规硶鏄?code>Thread绫荤殑涓€涓潤鎬佹柟娉曘€傚畠鐨勪綔鐢ㄦ槸璁╁綋鍓嶇嚎绋嬬潯鐪犱竴娈垫椂闂达細
Thread.sleep(long)
sleep
鏂规硶鍜?code>wait鏂规硶鐨勫尯鍒細
wait
鍙互鎸囧畾鏃堕棿锛屼篃鍙互涓嶆寚瀹氾紱鑰?code>sleep蹇呴』鎸囧畾鏃堕棿銆?/li>
wait
閲婃斁cpu璧勬簮锛屽悓鏃堕噴鏀鹃攣锛?code>sleep閲婃斁cpu璧勬簮锛屼絾鏄笉閲婃斁閿侊紝鎵€浠ユ槗姝婚攣銆?/li>
wait
蹇呴』鏀惧湪鍚屾鍧楁垨鍚屾鏂规硶涓紝鑰?code>sleep鍙互鍐嶄换鎰忎綅缃?/li>
馃幆ThreadLocal绫?/h2>
ThreadLocal
鏄竴涓湰鍦扮嚎绋嬪壇鏈彉閲忓伐鍏风被锛屽彲浠ョ悊瑙f垚涓虹嚎绋嬫湰鍦板彉閲忔垨绾跨▼鏈湴瀛樺偍銆備弗鏍兼潵璇达紝ThreadLocal
绫诲苟涓嶅睘浜庡绾跨▼闂寸殑閫氫俊锛岃€屾槸璁╂瘡涓嚎绋嬫湁鑷繁鈥滅嫭绔嬧€濈殑鍙橀噺锛岀嚎绋嬩箣闂翠簰涓嶅奖鍝嶃€?/p>
ThreadLocal
绫绘渶甯哥敤鐨勫氨鏄痵et鏂规硶鍜実et鏂规硶銆傜ず渚嬩唬鐮侊細public class mythread {
static class ThreadA implements Runnable {
private ThreadLocal<String> threadLocal;
public ThreadA(ThreadLocal<String> threadLocal) {
this.threadLocal = threadLocal;
}
@Override
public void run() {
threadLocal.set("A");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ThreadA杈撳嚭锛? + threadLocal.get());
}
public static void main(String[] args) {
ThreadLocal<String> threadLocal = new ThreadLocal<>();
new Thread(new ThreadA(threadLocal)).start();
}
}
}
ThreadLocal
锛岃€屼笉鏄湪姣忎釜绾跨▼涓0鏄庝竴涓鏈夊彉閲忔潵鎿嶄綔锛屽姞鈥滈噸鈥濈嚎绋嬨€?/p>
InheritableThreadLocal
鏄?code>ThreadLocal鐨勭户鎵垮瓙绫伙紝涓嶄粎褰撳墠绾跨▼鍙互瀛樺彇鍓湰鍊硷紝鑰屼笖瀹冪殑瀛愮嚎绋嬩篃鍙互瀛樺彇杩欎釜鍓湰鍊笺€?/p>
馃幆淇″彿閲忔満鍒?/h2>
馃幆绠¢亾
PipedReader
銆?PipedOutputStream
銆?PipedInputStream
銆傚叾涓紝鍓嶉潰涓や釜鏄熀浜庡瓧绗︾殑锛屽悗闈袱涓槸鍩轰簬瀛楄妭娴佺殑銆?/p>
public class Pipe {
static class ReaderThread implements Runnable {
private PipedReader reader;
public ReaderThread(PipedReader reader) {
this.reader = reader;
}
@Override
public void run() {
System.out.println("this is reader");
int receive = 0;
try {
while ((receive = reader.read()) != -1) {
System.out.print((char)receive);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
static class WriterThread implements Runnable {
private PipedWriter writer;
public WriterThread(PipedWriter writer) {
this.writer = writer;
}
@Override
public void run() {
System.out.println("this is writer");
int receive = 0;
try {
writer.write("test");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws IOException, InterruptedException {
PipedWriter writer = new PipedWriter();
PipedReader reader = new PipedReader();
writer.connect(reader); // 杩欓噷娉ㄦ剰涓€瀹氳杩炴帴锛屾墠鑳介€氫俊
new Thread(new ReaderThread(reader)).start();
Thread.sleep(1000);
new Thread(new WriterThread(writer)).start();
}
}
// 杈撳嚭锛?
this is reader
this is writer
test
鑷磋阿
閾炬帴锛歨ttps://juejin.cn/post/7050064181680144392