每個人都曾試圖在平淡的學(xué)習(xí)、工作和生活中寫一篇文章。寫作是培養(yǎng)人的觀察,、聯(lián)想,、想象、思維和記憶的重要手段,。大家想知道怎么樣才能寫一篇比較優(yōu)質(zhì)的范文嗎,?下面是小編幫大家整理的優(yōu)質(zhì)范文,僅供參考,,大家一起來看看吧,。
java線程池源碼解析篇一
java對象實例的鎖一共有四種狀態(tài):無鎖,偏向鎖,,輕量鎖和重量鎖,。原始脫離框架的并發(fā)應(yīng)用大部分都需要手動完成加鎖釋放,,最直接的就是使用synchronized和volatile關(guān)鍵字對某個對象或者代碼塊加鎖從而限制每次訪問的次數(shù),從對象之間的競爭也可以實現(xiàn)到對象之間的'協(xié)作,。但是這樣手動實現(xiàn)出來的應(yīng)用不僅耗費時間而且性能表現(xiàn)往往又有待提升,。
一、線程池結(jié)構(gòu)圖
二,、示例
定義線程接口
6public class mythread extends thread {@overridepublicvoid run() {n(tthread().getname() + "正在執(zhí)行");}}
1:newsinglethreadexecutor
10executorservice pool = executors. newsinglethreadexecutor();thread t1 = new mythread();thread t2 = new mythread();thread t3 = new mythread();//將線程放入池中進(jìn)行執(zhí)行e(t1);e(t2);e(t3);//wn();
輸入結(jié)果:
3pool-1-thread-1正在執(zhí)行pool-1-thread-1正在執(zhí)行pool-1-thread-1正在執(zhí)行
2:newfixedthreadpool
13executorservice pool = edthreadpool(3);thread t1 = new mythread();thread t2 = new mythread();thread t3 = new mythread();thread t4 = new mythread();thread t5 = new mythread();//將線程放入池中進(jìn)行執(zhí)行e(t1);e(t2);e(t3);e(t4);e(t5);wn();
輸入結(jié)果:
4pool-1-thread-1正在執(zhí)行pool-1-thread-2正在執(zhí)行pool-1-thread-1正在執(zhí)行pool-1-thread-2正在執(zhí)行
3 :newcachedthreadpool
14executorservice pool = hedthreadpool();thread t1 = new mythread();thread t2 = new mythread();thread t3 = new mythread();thread t4 = new mythread();thread t5 = new mythread();//將線程放入池中進(jìn)行執(zhí)行e(t1);e(t2);e(t3);e(t4);e(t5);//wn();
輸入結(jié)果:
5pool-1-thread-2正在執(zhí)行pool-1-thread-4正在執(zhí)行pool-1-thread-3正在執(zhí)行pool-1-thread-1正在執(zhí)行pool-1-thread-5正在執(zhí)行
4 :scheduledthreadpoolexecutor
14scheduledexecutorservice pool = eduledthreadpool(2);leatfixedrate(new runnable() {//每隔一段時間就觸發(fā)異常 @override public void run() { //throw new runtimeexception(); n("================"); }}, 1000, 2000, econds);leatfixedrate(new runnable() {//每隔一段時間打印系統(tǒng)時間,,證明兩者是互不影響的 @override public void run() { n("+++++++++++++++++"); }}, 1000, 2000, econds)
s("content_relate");【java線程池框架解析方法】相關(guān)文章:
1.
什么是java線程池框架
2.java 5線程池使用
3.java中通用的線程池實例代碼
4.java多線程之線程間的通信方式解析
5.java單線程多線程的實現(xiàn)與方法
6.java多線程介紹
7.java多線程教程
8.java 集合框架