开发者

Setting Interrupt Reason while interrupting a Thread

Thanks for all the help and support , I am facing a problem where by i have two threads one is a Timer thread , and another one is some File Reader thread. Now my main program is calling both the threads. The timer thread interrupts the main program on timeout , and the File Reader thread invokes the main thread if there are some IO errors. So now the problem step开发者_Python百科s in , the Main program has to know who has fired the interrupt , to print some interrupt status lets say. So how do i acheive this? Below are few things i dont want to use.

  1. A flag which is used to set on Timeout and another flag for IO error (Because My main program is huge and has several parts and i cant do this check everywhere)
  2. Each thread having a member variable set with status code , and the main program reading that on interrupt.(I am ok with this, but i still need to maintain the thread objects to get the status , and my File Reader threads are many , so i have to iterate every thread to find the one interrupted).

I would appreciate your help on this , even some other way other than interrupts is also fine.


I do it by declaring an Exception variable, and then I rethrow the exception if the waiting thread gets an interrupt which the Exception variable is set:

IOException ioex;

synchronized(lockObject) {
    while( true ) {
        try {
            lockObject.wait();
        } catch( InterruptedException e ) {
            if( ioex != null ) { 
                throw ioex;
            }
        }
        ... normal handling ...
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜