Skip to content

Instantly share code, notes, and snippets.

@jairamc
Created June 17, 2014 15:38
Show Gist options
  • Save jairamc/1f9ae535332b358bc47d to your computer and use it in GitHub Desktop.
Save jairamc/1f9ae535332b358bc47d to your computer and use it in GitHub Desktop.
Custom shutdown in a Runnable
package com.datasift.playback
import java.util.concurrent.Executors
class RunnableWithShutdown extends MyRunnable {
@volatile
var isShutDown = false
override def shutdown() { isShutDown = true }
override def run() {
while(!isShutDown) {
// Do Something
}
}
}
trait MyRunnable extends Runnable {
def shutdown()
}
class ThreadTest extends App {
val executor = Executors.newFixedThreadPool(3)
val myThreads = for (i <- 1 to 3) yield new RunnableWithShutdown
executor.submit(new MyThread)
myThreads foreach { _.shutdown }
executor.shutdown()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment