Skip to content

Instantly share code, notes, and snippets.

@artempyanykh
Created October 19, 2018 11:48
Show Gist options
  • Save artempyanykh/23672b174975477200f1ca07569f81c0 to your computer and use it in GitHub Desktop.
Save artempyanykh/23672b174975477200f1ca07569f81c0 to your computer and use it in GitHub Desktop.
import java.lang.management.ManagementFactory
import java.util.concurrent.ThreadPoolExecutor
import scalaz.zio.RTS
//import scalaz.zio.IO
object TheadPoolTestApp {
def main(args: Array[String]): Unit = {
val rts = new RTS {}
val pool = rts.threadPool.asInstanceOf[ThreadPoolExecutor]
// This is run in a separate thread to output diagnostic information,
// such as thread pool size and work queue size
val diagnosticPrinter: Runnable = () => {
var cycle = 0
val threadBean = ManagementFactory.getThreadMXBean()
while (true) {
cycle += 1
print(s"Cycle $cycle")
print(s" thread count = ${threadBean.getThreadCount()}")
print(s", queue size: ${ pool.getQueue.size()}")
println(s", task count: ${pool.getCompletedTaskCount} out of ${pool.getTaskCount} completed")
Thread.sleep(1000L)
}
}
println(s"Available CPUs: ${Runtime.getRuntime.availableProcessors()}")
println(s"Thread pool size: ${rts.threadPool.asInstanceOf[ThreadPoolExecutor].getCorePoolSize}")
val threadCounter = new Thread(diagnosticPrinter)
threadCounter.start()
Thread.sleep(1)
println("Start queueing load (> poolSize)")
val loadVolume = 100
1.to(loadVolume).foreach { _ =>
rts.submit {
Thread.sleep(2000L)
}
Thread.sleep(50L) // allow for some overlap between long running tasks
}
// Wait for all tasks to finish
while (pool.getCompletedTaskCount < loadVolume) {
Thread.sleep(1000L)
}
println("Start light load (< poolSize)")
1.to(5).foreach { _ =>
rts.submit(Thread.sleep(2000L))
Thread.sleep(2000L)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment