Forked from viktorklang/InterruptibleCancellableFuture.scala
Last active
January 3, 2016 03:29
-
-
Save dwhjames/8402617 to your computer and use it in GitHub Desktop.
Revisions
-
dwhjames revised this gist
Jan 17, 2014 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,15 +7,15 @@ import java.util.concurrent.atomic.AtomicReference val aref = new AtomicReference[Thread](null) p tryCompleteWith Future { val thread = Thread.currentThread aref.set(thread) try fun(f) finally { val wasInterrupted = (aref getAndSet null) ne thread //Deal with interrupted flag of this thread in desired } } (f, () => { Option(aref getAndSet null) foreach { _.interrupt() } p.tryFailure(new CancellationException) }) } -
dwhjames revised this gist
Jan 13, 2014 . 1 changed file with 5 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,26 +1,21 @@ import scala.concurrent._ import java.util.concurrent.atomic.AtomicReference def interruptableFuture[T](fun: Future[T] => T)(implicit ex: ExecutionContext): (Future[T], () => Boolean) = { val p = Promise[T]() val f = p.future val aref = new AtomicReference[Thread](null) p tryCompleteWith Future { val thread = Thread.currentThread aref.synchronized { aref.set(thread) } try fun(f) finally { val wasInterrupted = (aref.synchronized { aref getAndSet null }) ne thread //Deal with interrupted flag of this thread in desired } } (f, () => { aref.synchronized { Option(aref getAndSet null) foreach { _.interrupt() } } p.tryFailure(new CancellationException) }) } -
viktorklang revised this gist
Dec 13, 2013 . 1 changed file with 10 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,21 +1,26 @@ import scala.concurrent._ def interruptableFuture[T](fun: Future[T] => T)(implicit ex: ExecutionContext): (Future[T], () => Boolean) = { val p = Promise[T]() val f = p.future val lock = new Object var currentThread: Thread = null def updateCurrentThread(newThread: Thread): Thread = { val old = currentThread currentThread = newThread old } p tryCompleteWith Future { val thread = Thread.currentThread lock.synchronized { updateCurrentThread(thread) } try fun(f) finally { val wasInterrupted = lock.synchronized { updateCurrentThread(null) } ne thread //Deal with interrupted flag of this thread in desired } } (f, () => { lock.synchronized { Option(updateCurrentThread(null)) foreach { _.interrupt() } } p.tryFailure(new CancellationException) }) } -
viktorklang created this gist
Apr 18, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ import scala.concurrent._ import java.util.concurrent.atomic.AtomicReference def interruptableFuture[T](fun: Future[T] => T)(implicit ex: ExecutionContext): (Future[T], () => Boolean) = { val p = Promise[T]() val f = p.future val aref = new AtomicReference[Thread](null) p tryCompleteWith Future { val thread = Thread.currentThread aref.synchronized { aref.set(thread) } try fun(f) finally { val wasInterrupted = (aref.synchronized { aref getAndSet null }) ne thread //Deal with interrupted flag of this thread in desired } } (f, () => { aref.synchronized { Option(aref getAndSet null) foreach { _.interrupt() } } p.tryFailure(new CancellationException) }) }