¿ìËÙÒµÎñͨµÀ

¹ØÓÚ½â¾öJava±à³ÌÓïÑÔÏß³ÌÎÊÌâµÄ½¨Òé - ±à³ÌÈëÃÅÍø

×÷Õß ØýÃû¼¼Êõ À´Ô´ NET±à³Ì ä¯ÀÀ ·¢²¼Ê±¼ä 2012-06-20
¸ö active ¶ÔÏóÒÔÒ»ÖÖ¸üΪ¹ý³Ì»¯µÄÄ£ÐÍ¿ÉÒÔÏû³ý´ó¶àÊýµÄͬ²½ÎÊÌâ¡£

ÔÚijÖÖÒâÒåÉÏ£¬Java ±à³ÌÓïÑÔµÄÕû¸ö Swing/AWT ×ÓϵͳÊÇÒ»¸ö active ¶ÔÏó¡£ÏòÒ»¸ö Swing ¶ÓÁд«ËÍÒ»ÌõѶϢµÄΨһ°²È«µÄ;¾¶ÊÇ£¬µ÷ÓÃÒ»¸öÀàËÆ SwingUtilities.invokeLater() µÄ·½·¨£¬ÕâÑù¾ÍÔÚ Swing ʼþ¶ÓÁÐÉÏ·¢ËÍÁËÒ»¸ö runnable ¶ÔÏ󣬵±ÂÖµ½ËüÖ´ÐÐʱ£¬ Swing ʼþ´¦ÀíÏ߳̽«»á´¦ÀíËü¡£

ÄÇôÎҵĵÚÒ»¸ö½¨ÒéÊÇ£¬Ïò Java ±à³ÌÓïÑÔÖмÓÈëÒ»¸ö task £¨ÈÎÎñ£©µÄ¸ÅÄ´Ó¶ø½«active ¶ÔÏ󼯳ɵ½ÓïÑÔÖС£( taskµÄ¸ÅÄîÊÇ´Ó Intel µÄ RMX ²Ù×÷ϵͳºÍ Ada ±à³ÌÓïÑÔ½è¼ø¹ýÀ´µÄ¡£´ó¶àÊýʵʱ²Ù×÷ϵͳ¶¼Ö§³ÖÀàËƵĸÅÄî¡££©

¹ØÓÚ½â¾öJava±à³ÌÓïÑÔÏß³ÌÎÊÌâµÄ½¨Òé(2)

ʱ¼ä:2011-01-24 Allen Holub

Ò»¸öÈÎÎñÓÐÒ»¸öÄÚÖÃµÄ active ¶ÔÏó·Ö·¢³ÌÐò£¬²¢×Ô¶¯¹ÜÀíÄÇЩ´¦ÀíÒì²½ÐÅÏ¢µÄÈ«²¿»úÖÆ¡£

¶¨ÒåÒ»¸öÈÎÎñºÍ¶¨ÒåÒ»¸öÀà»ù±¾Ïàͬ£¬²»Í¬µÄÖ»ÊÇÐèÒªÔÚÈÎÎñµÄ·½·¨Ç°¼ÓÒ»¸ö asynchronous ÐÞÊηûÀ´Ö¸Ê¾ active ¶ÔÏóµÄ·ÖÅä³ÌÐòÔÚºǫ́´¦ÀíÕâЩ·½·¨¡£Çë²Î¿¼ÎÒµÄÊéÖеھÅÕµĻùÓÚÀà·½·¨£¬ÔÙ¿´ÒÔÏ嵀 file_io À࣬ËüʹÓÃÁËÔÚ¡¶ Taming Java Threads ¡·ÖÐËùÌÖÂÛµÄ Active_object ÀàÀ´ÊµÏÖÒì²****Ù×÷£º

interface Exception_handler ¡¡¡¡¡¡¡¡{¡¡ void handle_exception( Throwable e ); } ¡¡¡¡¡¡¡¡class File_io_task {¡¡ Active_object dispatcher = new Active_object(); ¡¡¡¡¡¡¡¡final OutputStream¡¡¡¡¡¡file; ¡¡¡¡¡¡¡¡final Exception_handler handler; ¡¡¡¡¡¡¡¡File_io_task( String file_name, Exception_handler handler ) ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡throws IOException ¡¡¡¡¡¡¡¡{¡¡ file = new FileOutputStream( file_name ); ¡¡¡¡¡¡¡¡¡¡¡¡this.handler = handler; ¡¡¡¡¡¡¡¡} ¡¡¡¡¡¡¡¡public void write( final byte[] bytes ) { ¡¡¡¡¡¡¡¡¡¡¡¡// The following call asks the active-object dispatcher ¡¡¡¡¡¡¡¡¡¡¡¡// to enqueue the Runnable object on its request ¡¡¡¡¡¡¡¡¡¡¡¡// queue. A thread associated with the active object ¡¡¡¡¡¡¡¡¡¡¡¡// dequeues the runnable objects and executes them ¡¡¡¡¡¡¡¡¡¡¡¡// one at a time. ¡¡¡¡¡¡¡¡¡¡¡¡dispatcher.dispatch ¡¡¡¡¡¡¡¡¡¡¡¡(¡¡ new Runnable() ¡¡¡¡¡¡¡¡{¡¡ public void run() { ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡try ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡{¡¡ byte[] copy new byte[ bytes.length ]; ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡System.arrayCopy(¡¡ bytes,¡¡0, ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡copy,¡¡ 0, ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡bytes.length ); ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡file.write( copy ); ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡} ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡catch( Throwable problem ) ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡{¡¡ handler.handle_exception( problem ); ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡} ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡} ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡} ¡¡¡¡¡¡¡¡¡¡¡¡); ¡¡¡¡¡¡¡¡} ¡¡¡¡}

ËùÓеÄдÇëÇó¶¼ÓÃÒ»¸ö dispatch() ¹ý³Ìµ÷Óñ»·ÅÔÚ active-object µÄÊäÈë¶ÓÁÐÖÐÅŶӡ£ÔÚºǫ́´¦ÀíÕâЩÒì²½ÐÅϢʱ³öÏÖµÄÈκÎÒì³£ (exception) ¶¼ÓÉ Exception_handler ¶ÔÏó´¦Àí£¬´Ë Exception_handler ¶ÔÏó±»´«Ë͵½ File_io_task µÄ¹¹Ô캯ÊýÖС£ÄúҪдÄÚÈݵ½Îļþʱ£¬´úÂëÈçÏ£º

File_io_task io =¡¡ new File_io_task ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ( "foo.txt" ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ new Exception_handler ¡¡¡¡¡¡¡¡ {¡¡ public void handle( Throwable e ) {¡¡ e.printStackTrace(); ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ } ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ } ¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡¡ ); ¡¡¡¡

ÁèÖڿƼ¼×¨ÒµÌṩ·þÎñÆ÷×âÓᢷþÎñÆ÷Íйܡ¢ÆóÒµÓʾ֡¢ÐéÄâÖ÷»úµÈ·þÎñ£¬¹«Ë¾ÍøÕ¾£ºhttp://www.lingzhong.cn ΪÁ˸ø¹ã´ó¿Í»§Á˽â¸ü¶àµÄ¼¼ÊõÐÅÏ¢£¬±¾¼¼ÊõÎÄÕÂÊÕ¼¯À´Ô´ÓÚÍøÂç,ÁèÖڿƼ¼×ðÖØÎÄÕÂ×÷ÕߵİæȨ£¬Èç¹ûÓÐÉæ¼°ÄãµÄ°æȨÓбØҪɾ³ýÄãµÄÎÄÕ£¬ÇëºÍÎÒÃÇÁªÏµ¡£ÒÔÉÏÐÅÏ¢ÓëÎÄÕÂÕýÎÄÊDz»¿É·Ö¸îµÄÒ»²¿·Ö,Èç¹ûÄúҪתÔر¾ÎÄÕÂ,Çë±£ÁôÒÔÉÏÐÅÏ¢£¬Ð»Ð»!

·ÖÏíµ½£º ¸ü¶à

Copyright ©1999-2011 ÏÃÃÅÁèÖڿƼ¼ÓÐÏÞ¹«Ë¾ ÏÃÃÅÓÅͨ»¥Áª¿Æ¼¼¿ª·¢ÓÐÏÞ¹«Ë¾ All rights reserved

µØÖ·(ADD)£ºÏÃÃÅÈí¼þÔ°¶þÆÚÍûº£Â·63ºÅ701E£¨¶«ÄÏÈÚͨÅÔ£© Óʱà(ZIP)£º361008

µç»°£º0592-5908028 ´«Õ棺0592-5908039 ×ÉѯÐÅÏ䣺web@lingzhong.cn ×ÉѯOICQ£º173723134

¡¶ÖлªÈËÃñ¹²ºÍ¹úÔöÖµµçÐÅÒµÎñ¾­ÓªÐí¿ÉÖ¤¡·ÃöB2-20100024  ICP±¸°¸:ÃöICP±¸05037997ºÅ