dnrajugade / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Threads.checkNotInterrupted() throws InterruptedException or similar #1219

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Suppose I'm implementing a CPU-expensive background operation, and I need to 
cancel it fast if the thread running it gets interrupted.
I would repeatedly write:

if(Thread.interrupted())
    throw new InterruptedException();

It would be nice to have that as a library method, such as:

Threads.checkNotInterrupted();

That would just implement the same behavior of interruptible system calls, so I 
think it may deserve getting into Guava.

Original issue reported on code.google.com by cairomas...@gmail.com on 1 Dec 2012 at 11:08

GoogleCodeExporter commented 9 years ago
See http://docs.oracle.com/javase/7/docs/api/java/lang/InterruptedException.html

Chris found a surprising number of people who do this check. However, is it 
worth introducing an entirely new class (Threads) and a new method just to 
avoid 3 lines of code (that's explicitly recommended in InterruptedException 
already?).

Naming this method would be tricky too.

Original comment by kak@google.com on 22 Aug 2013 at 11:41

GoogleCodeExporter commented 9 years ago
Yeah, a reasonable request, but it's a small enough snippet of code that I'd 
prefer to avoid introducing a new class.

Original comment by cpov...@google.com on 22 Aug 2013 at 11:43

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08