¡¡¡¡¡¡¡¡µ÷Óò»Í¬£ºRunnable¸üΪ»ù´¡£¬ThreadÖ»ÄÜ´«Runnable¶ÔÏó¡£RunnableʵÀý¶ÔÏó½»ÓÉFutureTask¶ÔÏóÀ´¹ÜÀí»òÕßʹÓÃExecutorServiceµÄsubmit·µ»ØÒ»¸ö¹ú¶¨µÄ½á¹û£¬½á¹ûÊÇ×÷Ϊ²ÎÊý´«ÈëµÄ¡£¶øCallable·µ»ØµÄ½á¹ûÊdzÌÐòÖ´ÐкóµÄ½á¹û¡£ExecutorServiceÔÚRunnable´«Èëʱ¿É½ÓÊÜÎÞ½á¹ûµÄ²ÎÊý£¬µ÷ÓÃsubmitĬÈÏ·µ»Ø¿Õ´®¡£
¡¡¡¡¡î¡¡²¢ÐÐ֪ʶµã2£ºFutureTaskºÍFutureµÄÒìͬ
¡¡¡¡¡¡¡¡¸ù±¾ÒìͬÔÚÓÚFutureÊǸö½Ó¿Ú£¬¶øFutureTaskʵÏÖÁËÕâ¸ö½Ó¿Ú£¬Í¬Ê±»¹ÊµÏÖÁËRunnable½Ó¿Ú¡£¶øFuture¶ÔÏó¶¨ÒåÁËcancel, isCancelled, isDone, getºÍÓг¬Ê±Ê±¼äµÄgetÎå¸ö·½·¨¡£ÆäÖÐcancel·½·¨£¬¿ÉÒÔ´«ÈëÒ»¸ö½Ð£º“ÊÇ·ñ¿ÉÒÔÖжϔµÄ²ÎÊý¡£FutureTask³ýÁËFutureÖ®Í⣬¾ÍÊÇrun·½·¨ÁË¡£run·½·¨Êǽ«Runnable»òÕßÊÇCallableµÄʵÀý¾ßÌåÖ´ÐеĹý³Ì¡£
¡¡¡¡¡î¡¡²¢ÐÐ֪ʶµã3£ºExecutorServiceµÄexecuteºÍsubmitµÄÒìͬ
¡¡¡¡¡¡¡¡¹¦ÄÜÏàͬ£º¶¼ÊÇÌí¼ÓÏ̵߳½Ï̳߳ØÖÐ
¡¡¡¡¡¡¡¡¹¦Äܲ»Í¬£ºsubmit·µ»Øfuture£¬¶øexecuteûÓзµ»ØÖµ¡£ÒòΪfuture¶ÔÏó¿ÉÒÔÈ¡Ïû£¬Ò²¿ÉÒÔÅжÏÊÇ·ñÈ¡Ïû£¬ÊÇ·ñÍê³É¡£Ò²¿ÉÒÔͨ¹ýfuture·µ»ØÖµÀ´Åжϡ£
¡¡¡¡¡î¡¡²¢ÐÐ֪ʶµã4£ºExecutorServiceµÄshutdownºÍshutdownNowµÄÇø±ð
¡¡¡¡¡¡¡¡µ±Ï̳߳ص÷ÓÃshutdown·½·¨Ê±£¬Ï̳߳ØÁ¢¼´±ä³Éshutdown״̬£¬²»ÔÙÍùÏ̳߳ØÀïÌí¼ÓÐÂÈÎÎñ£¬·ñÔò»áÅ׳öRejectedExecutionException£¬µ«ÊÇÏ̳߳ػáÔÚ³ØÖÐËùÓÐÈÎÎñ¶¼´¦ÀíÍê±Ï²ÅÍ˳ö¡£
µ±Ï̳߳ص÷ÓÃshutdownNow·½·¨Ê±£¬Ï̳߳ØÁ¢¼´±ä³Éstop״̬£¬²¢ÊÔͼֹͣËùÓÐÕýÔÚÖ´ÐеÄỊ̈߳¬²»ÔÙ´¦Àí²¢·µ»ØÏ̳߳ØÖеȴýµÄÈÎÎñ¡£ÒòΪֹͣÏß³ÌÊÇͨ¹ýThreadµÄÖжϷ½·¨À´ÊµÏÖ£¬ËùÒÔ²»Äܱ£Ö¤»áÁ¢¼´Í£Ö¹¡£
¡¡¡¡¡î¡¡²¢ÐÐ֪ʶµã5£ºjavaÖжϻúÖÆ
¡¡¡¡¡¡¡¡ÉÏÃæµÄfutureµÄcancel·½·¨²ÎÊýΪtrueºÍÏ̳߳صÄshutdownNow·½·¨¶¼Óõ½ÁËThread.interrupt()·½·¨¡£ËüÖ»ÊÇÉèÖñê־λ¡£ÐèÒªÏß³Ì×Ô¼ºÈ¥¶Ô±ê־λ¼ì²âºÍÏìÓ¦¡£ËüֻͣµôÏß³ÌÖÐÓÐsleep,wait,joinµÄ·½·¨¡£
¡¡¡¡×ö³ÌÐò×îÐèÒª¿¼ÂǵÄÊÂÇéÊÇʲôÄØ£¿µ±È»ÊÇÈçºÎ¸Ä½ø¡£Java8ÖÐÒýÈëlambdaº¯Êýʽ±à³ÌºÍÔöÇ¿Ð͵ÄCompletableFuture¡£¿´¿´Ê¹ÓÃÕâ¸öµÄÓï·¨£º
/** * <pre> * ********************************************************************************************************* * °æȨËùÓУ¨C£© 2010 BSNS * ±£ÁôËùÓÐȨÀû¡£ * .==. .==. * //'^\\ //^'\\ * // ^^\(\__/)/^ ^^\\ * //^ ^^ ^/6 6\ ^^^ \\ * //^ ^^ ^/( .. )\^ ^^ \\ * // ^^ ^/\|v""v|/\^^ ^ \\ * // ^^/\/ / '~~' \ \/\^ ^\\ * ---------------------------------------- * HERE BE DRAGONS WHICH CAN CREATE MIRACLE * ********************************************************************************************************* * »ù±¾ÐÅÏ¢ * ********************************************************************************************************* * ϵͳ£ºBRMAYI * Ä£¿é£ºBRMAYI CONCURRENT * ¹¦ÄÜ£ºÓÃExecutorServiceÀ´Ö´ÐÐFutureTask·µ»ØÖ´Ðнá¹ûµÄÒì²½Àý×Ó * ±àÂ룺¾²¶ù(xiexiaojing@le.com) * ʱ¼ä: 2010.10.13 * ********************************************************************************************************* * ÐÞ¸ÄÀúÊ· * ********************************************************************************************************* * ÐÞ¸ÄÕß ÐÞ¸ÄÄÚÈÝ ÐÞ¸Äʱ¼ä * ¾²¶ù(xiexiaojing@le.com) н¨ 2017.05.19 * ********************************************************************************************************* * </pre> */ public class Application { public static Map<String,String> call(String name){ Map<String,String> a = new HashMap<String,String>(); a.put(name, name); return a; } public static void main(String[] args) { try { final CompletableFuture<Map<String,String>> future1 = CompletableFuture.supplyAsync(()->call("name1")); final CompletableFuture<Map<String,String>> future2 = CompletableFuture.supplyAsync(()->call("name2")); System.out.println(future1.get(1, TimeUnit.MINUTES)); System.out.println(future2.get(1, TimeUnit.MINUTES)); } catch (Exception e) { e.printStackTrace(); } } }
¡¡¡¡ÒòΪÏÖÔÚÒѾÁ賿3µã¶à£¬Ô糿»¹ÒªÉϰ࣬½ñÌì·ÖÎöµÄºÜdz£¬Óпղ¹ÉÏ¡£½ö±í´ïÒ»ÖÖ¾³½ç£¬°üÀ¨ÎªÁË×·Çó¼¼Êõ²»Ãß²»ÐÝ:-)
¡¡