Java ²¢·¢Ñ§Ï°
Ò»¡¢´´½¨ÈÎÎñ
ÈÎÎñ¿ÉÒÔÀí½âΪһ¶Î¶ÀÁ¢Ö´ÐеĴúÂ룬ËüÍê³ÉÒ»¶¨µÄ¹¦ÄÜ¡£
ÔÚJava 5¼°ÒÔÉÏ°æ±¾Öв»½ö¿ÉÒÔÀûÓà Runnable½Ó¿Ú£¬»¹¿ÉÒÔÀûÓÃCallable½Ó¿Ú¡£
Á½ÖÖÈÎÎñµÄÇø±ð£ºRunnable µÄrun()·½·¨Íê³ÉµÄÈÎÎñÊÇûÓзµ»ØÖµµÄ - void
¶øCallable µÄcall()·½·¨Íê³ÉµÄÈÎÎñÊÇÓзµ»ØÖµµÄ - ·µ»ØÖµ ÓнӿڵķºÐÍ<V> À´Öƶ¨
¶þ¡¢Ö´ÐÐÈÎÎñ
ͨ¹ýjava.util.concurrent.ExecutorService½Ó¿Ú¶ÔÏóÀ´Ö´ÐÐÈÎÎñ£¬
Õâ¸ö½Ó¿Ú¶ÔÏóÓɹ¤¾ßÀàjava.util.concurrent.ExecutorsµÄ¾²Ì¬·½·¨À´´´½¨¡£
Ïà¹ØÀ༰½Ó¿Ú˵Ã÷£º
Executors Ìṩ½¨Á¢Ï̳߳ػòÏ̹߳¤³§µÄ·½·¨.
ExecutorService ÌṩÁ˹ÜÀíÖÕÖ¹µÄ·½·¨£¬ÒÔ¼°¿ÉΪ¸ú×ÙÒ»¸ö»ò¶à¸öÒì²½ÈÎÎñÖ´ÐÐ×´¿ö¶øÉú³É Future µÄ·½·¨¡£
Executor ¿ÉÒÔ¼òµ¥Àí½âΪһ¸öÏ̵߳ÄÖ´ÐÐÕߣ¬ÊÇExecutors µÄÉÏÒ»¼¶½Ó¿Ú¡£
Callable Ïß³ÌÒªÖ´ÐжøȥʵÏֵĽӿÚ
Future ±íʾÒì²½¼ÆËãµÄ½á¹û,¶ÔÖ´ÐеÄÏ̵߳Ä״̬¼ì²é,¿ÉÒÔ»ñµÃÖ´Ðнá¹û.
ÏÂÃæÒÔÒ»¸öÀý×ÓÀ´ËµÃ÷£º
public class CallableDemo {
public static void main(String[] args) {
/**
* Executors.newFixedThreadPool(5)´´½¨Ò»¸öÏ̳߳أ¬
* Õâ¸öÏ̳߳ØÖÐά»¤ 5 ¸öÏ̵߳ÄÖ´ÐÐÓëÏú»Ù¹¤×÷£¬²¢½«Î¬»¤µÄ¹¤×÷½»¸ø ExecutorService È¥´¦Àí
*/
ExecutorService service = Executors.newFixedThreadPool(5);
// ExecutorService service = Executors.newCachedThreadPool();
/**
* ¼Ç¼Ïß³ÌµÄ ÈÎÎñ£¨ÕâÀïÓÉʵÏÖÁËCallable½Ó¿Ú µÄÀàÀ´Ö´ÐУ©µÄ·µ»Ø½Ó¿Ú
*/
List<Future<String>> resultList = new ArrayList<Future<String>>();
/**
* ´´½¨ 10¸öÈÎÎñ
*/
for (int i = 0; i < 10; i++) {
/**
* ExecutorService µÄ submit·½·¨ À´Î¬»¤ÈÎÎñ(Õâ¸öÈÎÎñ²»ÊÇỊ̈߳¬ÊÇCallable£¬µ«ÊǺÍÏ̵߳Ť×÷ÔÀíÊÇÒ»ÑùµÄ)µÄÔËÐÐ״̬
*/
Future<String> future = service.submit(new
Ïà¹ØÎĵµ£º
package Utils.Sort;
/**
*ÀûÓÃðÅÝÅÅÐò·¨¶ÔÊý×éÅÅÐò£¬Êý×éÖÐÔªËرØÐëʵÏÖÁËComparable½Ó¿Ú¡£
*/
public class BubbleSort implements SortStrategy
{
/**
*¶ÔÊý×éobjÖеÄÔªËØÒÔðÅÝÅÅÐòËã·¨½øÐÐÅÅÐò
......
²ÎÊý˵Ã÷
fileName£º ±íʾÏë½âѹµÄÎļþÃû È磺c:\\aaa\a.zip
unZipDir£º ±íʾÏë½âѹµ½µÄ·¾¶ È磺c:\\ccc
public static void unZip1(String fileName, String unZipDir) {
try {
Fil ......
Ò»¡¢¼Ì³Ð
1.¸ÅÄһ¸öÀà¼Ì³ÐÁíÒ»¸öÀ࣬Ôò³Æ¼Ì³ÐµÄÀàΪ×ÓÀ࣬±»¼Ì³ÐµÄÀàΪ¸¸Àà¡£
2.Ä¿µÄ£ºÊµÏÖ´úÂëµÄ¸´Óá£
3.Àí½â£º×ÓÀàÓ븸ÀàµÄ¹Øϵ²¢²»ÊÇÈÕ³£Éú»îÖеĸ¸×Ó¹Øϵ£¬×ÓÀàÓ븸Àà¶øÊÇÒ»ÖÖÌØÊ⻯ÓëÒ»°ã»¯µÄ¹Øϵ£¬ÊÇis-aµÄ¹Øϵ£¬×ÓÀàÊǸ¸Àà¸ü¼ÓÏêϸµÄ·ÖÀà¡£Èçclass dog extends animal,¾Í¿ÉÒÔÀí½âΪdog is a animal.×¢ÒâÉè¼ ......
Ô·¢±íÓÚhttp://saharabear.javaeye.com
ºÍhttp://www.haulynjason.net(Ó¢)
ͬʱ·¢±íÔÚÕâÀï.
ÔÚÎÒÖÜΧ,ÓкܶàÈËÊÇJava³ÌÐòÔ±»òÕß¼ÆËã»úרҵµÄѧÉú.ÓÐʱ,ÎÒ°ïËûÃǵ÷ÊÔ³ÌÐòµÄʱºò,·¢ÏÖËûÃǵĻúÆ÷ÉÏ×ÜÊÇ°²×°×źܶàûÓÐÊÚȨµÄ¹¤¾ß,±ÈÈçµÁ°æµÄ
windows,ûÓÐLicenseµÄMyEclipseµÈ,ÉõÖÁÓÐһЩÈË·Ö²»ÇåeclipseºÍmyeclipse ......