【大学课件】实务与陷阱.ppt

上传人:sccc 文档编号:5632533 上传时间:2023-08-04 格式:PPT 页数:28 大小:747.01KB
返回 下载 相关 举报
【大学课件】实务与陷阱.ppt_第1页
第1页 / 共28页
【大学课件】实务与陷阱.ppt_第2页
第2页 / 共28页
【大学课件】实务与陷阱.ppt_第3页
第3页 / 共28页
【大学课件】实务与陷阱.ppt_第4页
第4页 / 共28页
【大学课件】实务与陷阱.ppt_第5页
第5页 / 共28页
点击查看更多>>
资源描述

《【大学课件】实务与陷阱.ppt》由会员分享,可在线阅读,更多相关《【大学课件】实务与陷阱.ppt(28页珍藏版)》请在三一办公上搜索。

1、,國立交通大學資訊工程學系分散式系統實驗室Distributed Computing Systems Lab,Java Thread Pool實務與陷阱,http:/,Agenda,Threads 數量上限容量滿載的處理策略異常處理天有不測風雲,http:/,參考資料,Scott Oaks&Henry Wong(2004),Java Threads,3rd edition,OReilly.Brian Goetz et.al(2006),Java Concurrency in Practice,Addison-Wesley.,http:/,Threads數量上限,http:/,Demo 1,Na

2、tive thread 上限(C/C+),http:/,Demo 2,VM thread 上限(Java),http:/,Java Concurrency Facilities,http:/,java.lang,Keyword:synchronized,java.lang,Java Threads,1st Way,http:/,n,main,Java Threads,2nd Way,http:/,Thread,main,Thread 容量滿載,Win32 APICreateThread()return NULLJavaThrow java.lang.OutOfMemoryError,http:

3、/,容量滿載處理策略,http:/,http:/,MyWorker,main,Demo 3,固定容量的 Thread Pool,http:/,newFixedThreadPool,public class TestThreadPool private static final int POOL_SIZE=10;private static final int MAX_COUNT=10000;public static void main(String args)Executor pool=Executors.newFixedThreadPool(POOL_SIZE);for(int i=0;i

4、 MAX_COUNT;+i)pool.execute(new ExecutorDemo(i);class ExecutorDemo implements Runnable public ExecutorDemo(int sn)/*/public void run()/*/,http:/,Executors 懶人包,FixedThreadPool固定數量彈性不足CachedThreadPool自動伸縮(0Integer.MAX_VALUE)無法處理瞬間巨量,http:/,Demo 4,兼顧並行性與系統負載的 Thread Pool,http:/,ThreadPool DIY!,public cl

5、ass TestThreadPool private static final int MAX_POOL_SIZE=10;private static final int CORE_POOL_SIZE=5;private static final long THREAD_KEEPALIVE_TIME=60;private static final int QUEUE_SIZE=3;/public static void main(String args)Executor pool=Executors.newThreadPoolExecutor(CORE_POOL_SIZE,MAX_POOL_S

6、IZE,THREAD_KEEPALIVE_TIME,TimeUnit.SECONDS,new ArrayBlockingQueue(QUEUE_SIZE),new ThreadPoolExecutor.CallerRunsPolicy();/*/,http:/,異常處理,http:/,Demo 5,當 Thread Pool 遇見 exception,http:/,他抓得住我?,public class TestThreadPool/public static void main(String args)Executor pool=Executors.newFixedThreadPool(PO

7、OL_SIZE);for(int i=0;i MAX_COUNT;+i)try pool.execute(new ExecutorDemo(i);catch(Throwable e)/*/class ExecutorDemo implements Runnable public ExecutorDemo(int sn)/*/public void run()throw new RuntimeException();,http:/,Demo 6,除錯版:安裝自訂的 Thread.UncaughtExceptionHandler,http:/,安裝自訂的 ThreadFactory,public

8、class TestThreadPool/public static void main(String args)Executor pool=Executors.newThreadPoolExecutor(CORE_POOL_SIZE,MAX_POOL_SIZE,THREAD_KEEPALIVE_TIME,TimeUnit.SECONDS,new ArrayBlockingQueue(QUEUE_SIZE),new SafeThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy();/*/class SafeThreadFactory i

9、mplements ThreadFactory/*下一頁*/,http:/,自訂 UncaughtExceptionHandler,class SafeThreadFactory implements ThreadFactory Overridepublic Thread newThread(Runnable r)Thread thread=new Thread(r);thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()Overridepublic void uncaughtException(Thre

10、ad t,Throwable e)/);return thread;,http:/,Demo 7,懶人版:不讓 thread 丟出 exception,http:/,Thread 防呆策略,class ErrorProofThread implements Runnable/public void run()try/*main program logic here*/catch(Throwable ignore)/you can do nothing here,http:/,天有不測風雲,http:/,HeartbeatWatchdog,http:/,Any Questions?,http:/,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 建筑/施工/环境 > 农业报告


备案号:宁ICP备20000045号-2

经营许可证:宁B2-20210002

宁公网安备 64010402000987号