大学课件实务与陷阱

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

《大学课件实务与陷阱》由会员分享,可在线阅读,更多相关《大学课件实务与陷阱(28页珍藏版)》请在金锄头文库上搜索。

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

2、:/ 2VM thread 上限 (Java)http:/ Concurrency Facilitieshttp:/ )start( )sleep( )Objectwait( )notify( )notifyAll( )Keyword: synchronized崭崭耶耶且且檄檄朋朋矛矛获获便便瞻瞻刺刺邹邹烷烷扎扎啥啥辉辉所所请请峪峪烁烁张张徒徒优优疆疆收收坯坯雾雾刽刽愿愿抹抹皋皋釉釉引引【大大学学课课件件】实实务务与与陷陷阱阱【大大学学课课件件】实实务务与与陷陷阱阱java.langJava Threads, 1st Wayhttp:/ )start( )sleep( )nMyThreadru

3、n( )main氛氛畔畔肢肢搬搬广广锑锑新新陵陵花花寺寺象象虽虽榆榆磊磊焙焙貌貌卖卖烽烽劝劝侨侨渊渊朴朴抖抖怜怜墒墒卷卷蚁蚁妇妇翼翼瓤瓤似似墅墅【大大学学课课件件】实实务务与与陷陷阱阱【大大学学课课件件】实实务务与与陷陷阱阱java.langJava Threads, 2nd Wayhttp:/ )mainn11others赣赣给给哆哆董董煤煤骡骡及及七七负负渍渍葬葬禁禁梭梭造造坪坪托托临临桔桔靶靶作作旱旱截截毙毙扳扳葫葫等等困困币币腹腹够够兔兔拼拼【大大学学课课件件】实实务务与与陷陷阱阱【大大学学课课件件】实实务务与与陷陷阱阱Thread 容量滿載Win32 APICreateThread(

4、) return NULLJavaThrow java.lang.OutOfMemoryErrorhttp:/ interface Executorexecute( Runnable )XXX utility Executors factory newFixedThreadPool( )newCachedThreadPool( )newScheduledThreadPool( ) instantiate use use 例例苏苏狈狈暂暂扶扶词词南南翠翠稀稀案案拦拦翼翼疽疽阐阐痒痒彝彝幂幂汾汾饰饰锭锭瓣瓣抠抠畴畴琼琼股股槽槽全全串串刁刁沸沸氟氟懈懈【大大学学课课件件】实实务务与与陷陷阱阱【大大学

5、学课课件件】实实务务与与陷陷阱阱Demo 3固定容量的 Thread Poolhttp:/ 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 MAX_COUNT; +i)pool.execute( new Executor

6、Demo(i) );class ExecutorDemo implements Runnable public ExecutorDemo(int sn) /*/ public void run() /*/ http:/ 懶人包FixedThreadPool固定數量彈性不足CachedThreadPool自動伸縮 (0Integer.MAX_VALUE)無法處理瞬間巨量http:/ utility Executors factory newFixedThreadPool( )newCachedThreadPool( )newScheduledThreadPool( )葫葫掘掘秋秋讣讣梨梨戊戊俗俗

7、凳凳哩哩工工呀呀榔榔磐磐尖尖霉霉和和离离咨咨仗仗按按串串镑镑呛呛陵陵自自靡靡否否曲曲柠柠姜姜男男携携【大大学学课课件件】实实务务与与陷陷阱阱【大大学学课课件件】实实务务与与陷陷阱阱Demo 4兼顧並行性與系統負載的 Thread Poolhttp:/ DIY!public class 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;

8、private static final int QUEUE_SIZE = 3;/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 ThreadPoolExecutor.CallerRunsPolicy();/*/http:/ 5當 Thread Pool 遇見

9、exceptionhttp:/ class TestThreadPool /public static void main(String args) Executor pool =Executors.newFixedThreadPool(POOL_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) /*/ pub

10、lic void run() throw new RuntimeException(); http:/ 6除錯版:安裝自訂的 Thread.UncaughtExceptionHandlerhttp:/ ThreadFactorypublic class TestThreadPool /public static void main(String args) Executor pool = Executors.newThreadPoolExecutor(CORE_POOL_SIZE,MAX_POOL_SIZE,THREAD_KEEPALIVE_TIME, TimeUnit.SECONDS,new

11、 ArrayBlockingQueue(QUEUE_SIZE),new SafeThreadFactory(),new ThreadPoolExecutor.CallerRunsPolicy();/*/class SafeThreadFactory implements ThreadFactory /*下一頁*/ http:/ UncaughtExceptionHandlerclass SafeThreadFactory implements ThreadFactory Overridepublic Thread newThread(Runnable r)Thread thread = new

12、 Thread(r);thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler()Overridepublic void uncaughtException(Thread t, Throwable e) /);return thread;http:/ 7懶人版:不讓 thread 丟出 exceptionhttp:/ 防呆策略class ErrorProofThread implements Runnable /public void run()try /*main program logic here*/catch (Throwable ignore) / you can do nothing herehttp:/ Questions?http:/

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 资格认证/考试 > 自考

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号