<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    badqiu

    XPer
    隨筆 - 46, 文章 - 3, 評論 - 195, 引用 - 0
    數據加載中……

    rapid-framework工具類介紹一: 異步IO類

    在一些特殊的場合,我們可能需要使用異步的IO來大幅提高性能.
    如日志信息收集.

    而rapid-framework提供的異步IO類,使用生產者/消費者的多線程同步模式及Decorator模式,如同使用正常的IO一樣,只需套多一層AsyncWriter/AsyncOutputStream,即可將普通IO轉換為異步IO來使用.打開一個異步IO后,將會在后臺開啟一個異步的線程來寫數據.

    異步的Writer使用:

    1 BufferedWriter?writer? = ? new ?BufferedWriter( new ?AsyncWriter( new ?FileWriter( " c:/debug.log " )));???
    2 writer.write( " xxxxx " );??



    異步的OutputStream使用:

    1 BufferedOutputStream?output? = ? new ?BufferedOutputStream( new ?AsyncOutputStream( new ?FileOutputStream( " c:/debug.log " )));???
    2 output.write( " foo " .getBytes());??

    ?

    在output使用完確保output被close,因為在close時,會強制異步線程將數據全部寫入最終的targetOutput. 而調用flush()方法則是空操作,不會寫數據.

    異步IO使用tip(1):

    可以將BufferedWriter/BufferedOutputStream的緩沖區加大,以減少寫入次數.

    異步IO使用tip(2):

    在close異步IO時也放在一個單獨的線程中,因為在實際應用場景中,close異步IO可能是十分耗時的操作.

    AsyncWriter源碼:

    ??1?public?class?AsyncWriter?extends?Writer?{
    ??2?
    ??3?????private?static?Log?log?=?LogFactory.getLog(AsyncWriter.class);
    ??4?????
    ??5?????private?static?final?int?DEFAULT_QUEUE_CAPACITY?=?50000;
    ??6?????private?final?static?char[]?CLOSED_SIGNEL?=?new?char[0];
    ??7?????
    ??8?????private?Writer?out;
    ??9?????private?DataProcessorThread?dataProcessor;
    ?10?????private?boolean?isClosed?=?false;
    ?11?????private?BlockingQueue<char[]>?queue?;
    ?12?????
    ?13?????private?AsyncExceptinHandler?asyncExceptinHandler?=?new?DefaultAsyncExceptinHandler();
    ?14?????private?static?long?threadSeqNumber;
    ?15?????private?static?synchronized?long?nextThreadID()?{
    ?16?????????return?++threadSeqNumber;
    ?17?????}
    ?18?????
    ?19?????private?class?DataProcessorThread?extends?Thread?{
    ?20?????????
    ?21?????????private?boolean?enabled?=?true;
    ?22?????????private?boolean?hasRuned?=?false;
    ?23?????????DataProcessorThread()?{
    ?24?????????????super("AsyncWriter.DataProcessorThread-"+nextThreadID());
    ?25?????????????setDaemon(true);
    ?26?????????}
    ?27?
    ?28?????????public?void?run()?{
    ?29?????????????hasRuned?=?true;
    ?30?????????????while?(this.enabled?||?!queue.isEmpty())?{
    ?31?????????????????
    ?32?????????????????char[]?buf;
    ?33?????????????????try?{
    ?34?????????????????????buf?=?queue.take();
    ?35?????????????????}?catch?(InterruptedException?e)?{
    ?36?//????????????????????e.printStackTrace();
    ?37?????????????????????continue;
    ?38?????????????????}
    ?39?????????????????
    ?40?????????????????if(buf?==?CLOSED_SIGNEL)?{
    ?41?????????????????????return;
    ?42?????????????????}
    ?43?????????????????
    ?44?????????????????try?{
    ?45?????????????????????out.write(buf);
    ?46?????????????????}?catch?(IOException?e)?{
    ?47??????????????????????asyncExceptinHandler.handle(e);
    ?48?????????????????}
    ?49?????????????}
    ?50?????????}
    ?51?????}
    ?52?
    ?53?????public?AsyncWriter(Writer?out)?{
    ?54?????????this(out,DEFAULT_QUEUE_CAPACITY,Thread.NORM_PRIORITY?+?1);
    ?55?????}
    ?56?????
    ?57?????public?AsyncWriter(Writer?out,int?queueCapacity)?{
    ?58?????????this(out,queueCapacity,Thread.NORM_PRIORITY?+?1);
    ?59?????}
    ?60?????
    ?61?????public?AsyncWriter(Writer?out,int?queueCapacity,int?dataProcesserThreadPriority)?{
    ?62?????????this(out,new?ArrayBlockingQueue(queueCapacity),dataProcesserThreadPriority);
    ?63?????}
    ?64?????
    ?65?????public?AsyncWriter(Writer?out,BlockingQueue?queue,int?dataProcesserThreadPriority)?{
    ?66?????????if(out?==?null)?throw?new?NullPointerException();
    ?67?????????if(queue?==?null)?throw?new?NullPointerException();
    ?68?????????
    ?69?????????this.queue?=?queue;
    ?70?????????this.dataProcessor?=?new?DataProcessorThread();
    ?71?????????if(dataProcesserThreadPriority?!=?Thread.NORM_PRIORITY)?{
    ?72?????????????this.dataProcessor.setPriority(dataProcesserThreadPriority);
    ?73?????????}
    ?74?????????this.dataProcessor.start();
    ?75?????????this.out?=?out;
    ?76?????}
    ?77?????
    ?78?????public?AsyncWriter(Writer?out,AsyncExceptinHandler?handler)?{
    ?79?????????this(out);
    ?80?????????setAsyncExceptinHandler(handler);
    ?81?????}
    ?82?
    ?83?????public?void?write(char[]?buf,?int?offset,?int?length)?throws?IOException?{
    ?84?????????synchronized?(lock)?{
    ?85?????????????if(isClosed)?throw?new?IOException("already?closed");
    ?86?????????????try?{
    ?87?????????????????queue.put(BufferCopyUtils.copyBuffer(buf,?offset,?length));
    ?88?????????????}?catch?(InterruptedException?e)?{
    ?89?????????????????throw?new?IOException("AsyncWriter?occer?error",e);
    ?90?????????????}
    ?91?????????}
    ?92?????}
    ?93?
    ?94?????public?void?close()?throws?IOException?{
    ?95?????????synchronized?(lock)?{
    ?96?????????????try?{
    ?97?????????????????isClosed?=?true;
    ?98?????????????????dataProcessor.enabled?=?false;
    ?99?????????????????if(queue.isEmpty())?{
    100?????????????????????queue.offer(CLOSED_SIGNEL);
    101?????????????????}
    102?????????????????
    103?????????????????try?{
    104?????????????????????dataProcessor.join();
    105?????????????????}?catch?(InterruptedException?e)?{
    106?????????????????????//ignore
    107?????????????????}
    108?????????????????
    109?????????????????if(!dataProcessor.hasRuned)?{
    110?????????????????????dataProcessor.run();
    111?????????????????}
    112?????????????}finally?{
    113?????????????????out.close();
    114?????????????}
    115?????????}
    116?????}
    117?????
    118?????public?void?flush()?throws?IOException?{
    119?????}
    120?
    121?????protected?void?finalize()?throws?Throwable?{
    122?????????super.finalize();
    123?????????if(!isClosed)?{
    124?????????????log.warn("AsyncWriter?not?close:"+this);
    125?????????????close();
    126?????????}
    127?????}
    128?
    129?????public?void?setAsyncExceptinHandler(AsyncExceptinHandler?asyncExceptinHandler)?{
    130?????????if(asyncExceptinHandler?==?null)?throw?new?NullPointerException();
    131?????????this.asyncExceptinHandler?=?asyncExceptinHandler;
    132?????}
    133?
    134?}

    ?

    ?

    rapid-framework網站:
    http://code.google.com/p/rapid-framework

    ?

    在線javadoc:
    http://www.rapid-framework.org.cn/rapid-javadoc-v2.0.x/

    ?

    posted on 2009-05-08 01:22 badqiu 閱讀(1500) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
    博客園   IT新聞   Chat2DB   C++博客   博問  
     
    主站蜘蛛池模板: 黄色永久免费网站| 四虎国产成人永久精品免费| 成年在线观看网站免费| 亚洲国产婷婷六月丁香| 久青草视频在线观看免费| 亚洲伊人久久综合中文成人网| MM1313亚洲国产精品| 全部免费a级毛片| 色老头综合免费视频| 亚洲一级特黄大片无码毛片 | 噜噜综合亚洲AV中文无码| 女人让男人免费桶爽30分钟| 亚洲人成色777777精品| 日韩毛片免费在线观看| 国产av无码专区亚洲av毛片搜| 亚洲?V乱码久久精品蜜桃| 国产乱妇高清无乱码免费| 亚洲精品无码久久久影院相关影片| 91成人免费观看在线观看| 日韩精品一区二区亚洲AV观看| a拍拍男女免费看全片| 亚洲乱理伦片在线观看中字| www.亚洲一区| 午夜精品射精入后重之免费观看| 亚洲美女中文字幕| 国产人妖ts在线观看免费视频| 一道本不卡免费视频| 亚洲av日韩av无码| 国产成人A在线观看视频免费| 羞羞网站免费观看| 亚洲国产成人久久精品动漫| 丁香花免费完整高清观看| 男性gay黄免费网站| 亚洲成av人在线视| 两个人的视频高清在线观看免费 | 黄床大片免费30分钟国产精品| 亚洲AV无码精品色午夜在线观看| 免费观看激色视频网站(性色)| 国产成人亚洲精品91专区高清 | 人人爽人人爽人人片A免费| 国产精品亚洲一区二区三区在线|