Home | History | Annotate | Download | only in util
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.android.tradefed.util;
     17 
     18 import java.io.IOException;
     19 import java.net.Socket;
     20 import java.util.ArrayList;
     21 import java.util.List;
     22 
     23 /**
     24  * A thread which waits for a period of time and then interrupts a specific other thread.
     25  * Can call {@link Thread#interrupt()} to get a thread out of a blocking wait, or
     26  * {@link Socket#close()} to stop a thread from blocking on a socket read or write.
     27  * <p/>
     28  * All time units are in milliseconds.
     29  */
     30 public class Alarm extends Thread {
     31     private final List<Thread> mInterruptThreads = new ArrayList<Thread>();
     32     private final List<Socket> mInterruptSockets = new ArrayList<Socket>();
     33     private final long mTimeoutTime;
     34     private boolean mAlarmFired = false;
     35 
     36     /**
     37      * Constructor takes the amount of time to wait, in millis.
     38      *
     39      * @param timeout The amount of time to wait, in millis
     40      * @throws IllegalArgumentException if {@code timeout <= 0}.
     41      */
     42     public Alarm(long timeout) {
     43         setName(getClass().getCanonicalName());
     44         setDaemon(true);
     45 
     46         if (timeout <= 0) {
     47             throw new IllegalArgumentException(String.format(
     48                     "Alarm timeout time %d <= 0, which is not valid.", timeout));
     49         }
     50 
     51         mTimeoutTime = timeout;
     52     }
     53 
     54     public void addThread(Thread intThread) {
     55         mInterruptThreads.add(intThread);
     56     }
     57 
     58     public void addSocket(Socket intSocket) {
     59         mInterruptSockets.add(intSocket);
     60     }
     61 
     62     public boolean didAlarmFire() {
     63         return mAlarmFired;
     64     }
     65 
     66     @Override
     67     public void run() {
     68         try {
     69             Thread.sleep(mTimeoutTime);
     70         } catch (InterruptedException e) {
     71             // Expected; return without interrupt()ing any of our InterruptThreads
     72             return;
     73         }
     74         mAlarmFired = true;
     75         for (Socket sock : mInterruptSockets) {
     76             try {
     77                 sock.close();
     78             } catch (IOException e) {
     79                 // ignore
     80             }
     81         }
     82         for (Thread thread : mInterruptThreads) {
     83             thread.interrupt();
     84         }
     85     }
     86 }
     87 
     88