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

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

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

    Change Dir

    先知cd——熱愛生活是一切藝術的開始

    統計

    留言簿(18)

    積分與排名

    “牛”們的博客

    各個公司技術

    我的鏈接

    淘寶技術

    閱讀排行榜

    評論排行榜

    分享一個ThreadMonitor

    來自commons-io的一段小程序,感覺會有用,拿來分享一下:

       1: /*
       2:  * Licensed to the Apache Software Foundation (ASF) under one or more
       3:  * contributor license agreements.  See the NOTICE file distributed with
       4:  * this work for additional information regarding copyright ownership.
       5:  * The ASF licenses this file to You under the Apache License, Version 2.0
       6:  * (the "License"); you may not use this file except in compliance with
       7:  * the License.  You may obtain a copy of the License at
       8:  *
       9:  *      http://www.apache.org/licenses/LICENSE-2.0
      10:  *
      11:  * Unless required by applicable law or agreed to in writing, software
      12:  * distributed under the License is distributed on an "AS IS" BASIS,
      13:  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      14:  * See the License for the specific language governing permissions and
      15:  * limitations under the License.
      16:  */
      17: package org.apache.commons.io;
      18:  
      19: /**
      20:  * Monitors a thread, interrupting it of it reaches the specified timout.
      21:  * <p>
      22:  * This works by sleeping until the specified timout amount and then
      23:  * interrupting the thread being monitored. If the thread being monitored
      24:  * completes its work before being interrupted, it should <code>interrupt()<code>
      25:  * the <i>monitor</i> thread.
      26:  * <p>
      27:  * 
      28:  * <pre>
      29:  *       long timeoutInMillis = 1000;
      30:  *       try {
      31:  *           Thread monitor = ThreadMonitor.start(timeoutInMillis);
      32:  *           // do some work here
      33:  *           ThreadMonitor.stop(monitor);
      34:  *       } catch (InterruptedException e) {
      35:  *           // timed amount was reached
      36:  *       }
      37:  * </pre>
      38:  *
      39:  * @version  $Id: ThreadMonitor.java 1002689 2010-09-29 15:43:48Z niallp $
      40:  */
      41: class ThreadMonitor implements Runnable {
      42:  
      43:     private final Thread thread;
      44:     private final long timeout;
      45:  
      46:     /**
      47:      * Start monitoring the current thread.
      48:      *
      49:      * @param timeout The timout amount in milliseconds
      50:      * or no timeout if the value is zero or less
      51:      * @return The monitor thread or <code>null</code>
      52:      * if the timout amount is not greater than zero
      53:      */
      54:     public static Thread start(long timeout) {
      55:         return start(Thread.currentThread(), timeout);
      56:     }
      57:  
      58:     /**
      59:      * Start monitoring the specified thread.
      60:      *
      61:      * @param thread The thread The thread to monitor
      62:      * @param timeout The timout amount in milliseconds
      63:      * or no timeout if the value is zero or less
      64:      * @return The monitor thread or <code>null</code>
      65:      * if the timout amount is not greater than zero
      66:      */
      67:     public static Thread start(Thread thread, long timeout) {
      68:         Thread monitor = null;
      69:         if (timeout > 0) {
      70:             ThreadMonitor timout = new ThreadMonitor(thread, timeout);
      71:             monitor = new Thread(timout, ThreadMonitor.class.getSimpleName());
      72:             monitor.setDaemon(true);
      73:             monitor.start();
      74:         }
      75:         return monitor;
      76:     }
      77:  
      78:     /**
      79:      * Stop monitoring the specified thread.
      80:      *
      81:      * @param thread The monitor thread, may be <code>null</code>
      82:      */
      83:     public static void stop(Thread thread) {
      84:         if (thread != null) {
      85:             thread.interrupt();
      86:         }
      87:     }
      88:  
      89:     /**
      90:      * Construct and new monitor.
      91:      *
      92:      * @param thread The thread to monitor
      93:      * @param timeout The timout amount in milliseconds
      94:      */
      95:     private ThreadMonitor(Thread thread, long timeout) {
      96:         this.thread = thread;
      97:         this.timeout = timeout;
      98:     }
      99:  
     100:     /**
     101:      * Sleep until the specified timout amount and then
     102:      * interrupt the thread being monitored.
     103:      *
     104:      * @see Runnable#run()
     105:      */
     106:     public void run() {
     107:         try {
     108:             Thread.sleep(timeout);
     109:             thread.interrupt();
     110:         } catch (InterruptedException e) {
     111:             // timeout not reached
     112:         }
     113:     }
     114: }

     

    以上這段代碼,示例程序演示了使用,但是直接copy可能會看到紅叉,很簡單,your work應該是有InterruptedException異常拋出才好。

    檢查程序執行時間的代碼肯定所有人都寫過一大坨了,但是換個角度如何,我們給設定一個時間閾值timeout,然后運行這個monitor進程,判斷自己寫的程序是否超時。這個需求,我想很多人都是有的吧。

    簡單的示例

       1: long timeoutInMillis = 1000;
       2:         try {
       3:             Thread monitor = ThreadMonitor.start(timeoutInMillis);
       4:             //do your work
       5:             // 1, Thread.sleep(0);
       6:             // 2, calc();
       7:             ThreadMonitor.stop(monitor);
       8:         } catch (InterruptedException e) {
       9:             //catch the timeout work
      10:         }

    像注釋1一樣使用,是我的一個小trick,像注釋2那樣使用可能更好一些,因為直接調用方法,產生的5.9倍單元計算的開銷還是可以接受的。

    當然你自己的calc方法需要throws InterruptedException。

    posted on 2012-02-20 19:36 changedi 閱讀(2628) 評論(2)  編輯  收藏 所屬分類: 好代碼分享

    評論

    # re: 分享一個ThreadMonitor 2012-02-23 22:00 midstr

    為啥不用juc呢?  回復  更多評論   

    # re: 分享一個ThreadMonitor 2012-02-27 18:49 changedi

    @midstr
    一個最直接的原因~~簡單而且兼容低版本jdk  回復  更多評論   

    主站蜘蛛池模板: 一级毛片免费观看不卡视频| 黄色毛片免费观看| 国产精品99久久免费观看| 免费一区二区视频| 深夜A级毛片视频免费| 最好免费观看韩国+日本 | 思思久久99热免费精品6| 国产老女人精品免费视频 | 特黄特色的大片观看免费视频| 老司机永久免费网站在线观看| 亚洲AV永久无码精品网站在线观看| 特级淫片国产免费高清视频| 亚洲精品国产摄像头| 免费人成网站在线播放| 一个人看的www在线免费视频| 黑人大战亚洲人精品一区| a级毛片视频免费观看| 亚洲AV无码国产精品麻豆天美| 99久热只有精品视频免费观看17| 亚洲伊人tv综合网色| 成人a视频片在线观看免费| 久久久久久亚洲av无码蜜芽| 亚洲国产精品成人| 久久伊人免费视频| 亚洲熟妇无码AV| 久久国产成人精品国产成人亚洲| 玖玖在线免费视频| 亚洲国产综合第一精品小说| 午夜视频免费观看| 精品多毛少妇人妻AV免费久久| 亚洲av激情无码专区在线播放| 4hu四虎最新免费地址| 国产成人亚洲综合a∨| 在线亚洲精品福利网址导航| 日本不卡免费新一区二区三区| 亚洲伦理中文字幕| 亚洲精品动漫人成3d在线| 91人人区免费区人人| 色妞www精品视频免费看| 亚洲欧洲免费视频| 国产免费午夜a无码v视频|