Skip to content

Instantly share code, notes, and snippets.

View kermitas's full-sized avatar

Artur Stanek kermitas

View GitHub Profile
@kermitas
kermitas / ExampleOfLowPriorityThreadsDispatcherUsage.scala
Created July 6, 2015 11:28
Example of usage low priority threads Akka dispatcher.
val lowPriorityThreadsDispatcherName = "low-priority-threads-dispatcher"
val lowPriorityThreadsExecutionContext = play.libs.Akka.system.dispatchers.lookup(lowPriorityThreadsDispatcherName)
Future {
require(Thread.currentThread.getPriority == Thread.MIN_PRIORITY, s"current thread's priority should be ${Thread.MIN_PRIORITY} but it is ${Thread.currentThread.getPriority}")
1 + 1 // do something that would be executed on low pririty thread
}(lowPriorityThreadsExecutionContext)
@kermitas
kermitas / application.conf
Created July 6, 2015 11:23
Configuration for Akka dispatcher that contains low priority threads.
# Low priority threads protects system from CPU starvation.
low-priority-threads-dispatcher {
# For more details please see documentation of this class.
type = akka.dispatch.PriorityThreadsDispatcher
executor = "thread-pool-executor"
# Priority of threads in this thread pool, should be between Thread.MIN_PRIORITY (which is 1) and Thread.MAX_PRIORITY (which is 10).
thread-priority = 1
@kermitas
kermitas / PriorityThreadFactory.scala
Created July 6, 2015 11:19
ThreadFactory with configurable priority.
package akka.dispatch
import java.util.concurrent.ThreadFactory
/**
* Composition over the [[DispatcherPrerequisites.threadFactory]] that set priority for newly created threads.
*
* @param newThreadPriority priority that will be set to each newly created thread
* should be between Thread.MIN_PRIORITY (which is 1) and Thread.MAX_PRIORITY (which is 10)
*/
@kermitas
kermitas / PriorityThreadsDispatcherPrerequisites.scala
Created July 6, 2015 11:11
Prerequisites for Akka dispatcher.
package akka.dispatch
/**
* Composition over [[DefaultDispatcherPrerequisites]] that replaces thread factory with one that allow to configure thread priority.
*
* @param newThreadPriority priority that will be set to each newly created thread
* should be between Thread.MIN_PRIORITY (which is 1) and Thread.MAX_PRIORITY (which is 10)
*/
class PriorityThreadsDispatcherPrerequisites(prerequisites: DispatcherPrerequisites, newThreadPriority: Int) extends DefaultDispatcherPrerequisites(
eventStream = prerequisites.eventStream,
@kermitas
kermitas / PriorityThreadsDispatcher.scala
Last active January 30, 2019 13:47
Akka dispatcher with configurable priority for created threads.
package akka.dispatch
import com.typesafe.config.Config
object PriorityThreadsDispatcher {
/**
* Configuration key under which int value should be placed.
*/
val threadPriorityConfigKey = "thread-priority"
}