Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

when to use InterruptedException  RSS feed

 
Saha Kumar
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have written some code as follows. Please look at the comments in the code.

public void run(){
while (true){
try{
Thread.sleep(SOME_TIME);
myCache.lock.writeLock().lock();
//there is code here to remove cache elements
myCache.lock.writeLock().unlock();
}catch(InterruptedException ie){
//What goes here? I have read that one should use:
//Thread.currentThread.interrupt();
//But my question is: is there another way that the
// InterruptedException can be thrown other than when the
// Thread.sleep(SOME_TIME) has timed out? Could this
// thread be interrupted by another source...for
// example the thread is being shut down gracefully by an
// application server which contains this code?
}
}
}

The thread above is a thread which is used for garbage collection of a cache. The code is loaded statically and is started by some code called from a servlet.

My problem is: if I include Thread.currentThread.interrupt();
then I don't think the thread will ever be stopped because it resides within an application server. If I don't include Thread.currentThread.interrupt();
then I think that if an InterruptedException occurs, then the cache garbage collector will stop running (which is not good either).

Please let me know some solution and a discussion of this topic.

Thanks in advance.

-Saha
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Per the javadoc, you only get "InterruptedException - if another thread has interrupted the current thread." The other thread would look like:

What you do in response is up to you. Do you want that signal to terminate the loop? Do you want to ignore it?

BTW: Look into java.util.Timer. It's a good way to run a periodic task like this.
 
Saha Kumar
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Stan,

If the thread was interrupted by the system, for example to gracefully shutdown the cache cleaning, then I wanted to terminate the thread. If it was interrupted due to the action exceeded a time limit, then I want to loop and try to execute it again.

The problem for me is not knowing the source of the interrupt. I don't want the cache cleaner to stop running forever just because it timed out.

Thanks for your wisdom.

-Saha
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you set some instance field before you call interrupt(). You can put whatever data you want to into the field, and check the data after the interruption. Since the field will be used to communicate between threads, you should probably synchronize access to the field, or make it volatile.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!