Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save dwhjames/8402617 to your computer and use it in GitHub Desktop.

Select an option

Save dwhjames/8402617 to your computer and use it in GitHub Desktop.

Revisions

  1. dwhjames revised this gist Jan 17, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions InterruptibleCancellableFuture.scala
    Original 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.synchronized { aref.set(thread) }
    aref.set(thread)
    try fun(f) finally {
    val wasInterrupted = (aref.synchronized { aref getAndSet null }) ne thread
    val wasInterrupted = (aref getAndSet null) ne thread
    //Deal with interrupted flag of this thread in desired
    }
    }

    (f, () => {
    aref.synchronized { Option(aref getAndSet null) foreach { _.interrupt() } }
    Option(aref getAndSet null) foreach { _.interrupt() }
    p.tryFailure(new CancellationException)
    })
    }
    }
  2. dwhjames revised this gist Jan 13, 2014. 1 changed file with 5 additions and 10 deletions.
    15 changes: 5 additions & 10 deletions InterruptibleCancellableFuture.scala
    Original 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 lock = new Object
    var currentThread: Thread = null
    def updateCurrentThread(newThread: Thread): Thread = {
    val old = currentThread
    currentThread = newThread
    old
    }
    val aref = new AtomicReference[Thread](null)
    p tryCompleteWith Future {
    val thread = Thread.currentThread
    lock.synchronized { updateCurrentThread(thread) }
    aref.synchronized { aref.set(thread) }
    try fun(f) finally {
    val wasInterrupted = lock.synchronized { updateCurrentThread(null) } ne thread
    val wasInterrupted = (aref.synchronized { aref getAndSet null }) ne thread
    //Deal with interrupted flag of this thread in desired
    }
    }

    (f, () => {
    lock.synchronized { Option(updateCurrentThread(null)) foreach { _.interrupt() } }
    aref.synchronized { Option(aref getAndSet null) foreach { _.interrupt() } }
    p.tryFailure(new CancellationException)
    })
    }
  3. @viktorklang viktorklang revised this gist Dec 13, 2013. 1 changed file with 10 additions and 5 deletions.
    15 changes: 10 additions & 5 deletions InterruptibleCancellableFuture.scala
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,26 @@
    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)
    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
    aref.synchronized { aref.set(thread) }
    lock.synchronized { updateCurrentThread(thread) }
    try fun(f) finally {
    val wasInterrupted = (aref.synchronized { aref getAndSet null }) ne thread
    val wasInterrupted = lock.synchronized { updateCurrentThread(null) } ne thread
    //Deal with interrupted flag of this thread in desired
    }
    }

    (f, () => {
    aref.synchronized { Option(aref getAndSet null) foreach { _.interrupt() } }
    lock.synchronized { Option(updateCurrentThread(null)) foreach { _.interrupt() } }
    p.tryFailure(new CancellationException)
    })
    }
  4. @viktorklang viktorklang created this gist Apr 18, 2013.
    21 changes: 21 additions & 0 deletions InterruptibleCancellableFuture.scala
    Original 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)
    })
    }