Created
June 17, 2014 15:38
-
-
Save jairamc/1f9ae535332b358bc47d to your computer and use it in GitHub Desktop.
Custom shutdown in a Runnable
This file contains 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 characters
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