Skip to content

Instantly share code, notes, and snippets.

@jorgeortiz85
Last active December 15, 2015 16:19
Show Gist options
  • Save jorgeortiz85/5288381 to your computer and use it in GitHub Desktop.
Save jorgeortiz85/5288381 to your computer and use it in GitHub Desktop.
import com.twitter.util.{Duration, Future, FuturePool, Try}
import java.util.concurrent.{LinkedBlockingQueue, ThreadPoolExecutor, TimeUnit}
val poolSize = 4
val queue = new LinkedBlockingQueue[Runnable]()
val executor = new ThreadPoolExecutor(poolSize, poolSize, 0L, TimeUnit.MILLISECONDS, queue)
val futurePool = FuturePool(executor)
def doWork(n: Int): Int = {
Thread.sleep(1000)
n
}
def hogThread(n: Int): Future[Int] = futurePool {
doWork(n)
(futurePool {
doWork(n)
}).get
}
val data1F = hogThread(1)
val data2F = hogThread(2)
val data3F = hogThread(3)
val data4F = hogThread(4)
val resultF =
for {
data1 <- data1F
data2 <- data2F
data3 <- data3F
data4 <- data4F
} yield {
data1 + data2 + data3 + data4
}
resultF.get(Duration.fromSeconds(3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment