mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-03-11 22:31:52 +09:00
Copyright update 2017
Former-commit-id: 8c662c7cb637ae4565e95e661b0e627d9bfd7e5a Former-commit-id: 4a5279b47f7c8f4fadb62ee6843b3848f803f433
This commit is contained in:
51
src/net/torvald/terrarum/concurrent/ThreadParallel.kt
Normal file
51
src/net/torvald/terrarum/concurrent/ThreadParallel.kt
Normal file
@@ -0,0 +1,51 @@
|
||||
package net.torvald.terrarum.concurrent
|
||||
|
||||
import net.torvald.terrarum.Terrarum
|
||||
import java.util.*
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 16-05-25.
|
||||
*/
|
||||
object ThreadPool {
|
||||
val POOL_SIZE = Terrarum.THREADS + 1
|
||||
|
||||
private val pool: Array<Thread?> = Array(POOL_SIZE, { null })
|
||||
|
||||
/**
|
||||
* Map array of Runnable objects to thread pool.
|
||||
* @param prefix : will name each thread as "Foo-1"
|
||||
* @param runnables : vararg
|
||||
*/
|
||||
fun mapAll(prefix: String, runnables: Array<Runnable>) {
|
||||
if (runnables.size != POOL_SIZE)
|
||||
throw RuntimeException("Thread pool argument size mismatch. If you have four cores, you must use four runnables.")
|
||||
|
||||
for (i in 0..runnables.size)
|
||||
pool[i] = Thread(runnables[i], "$prefix-$i")
|
||||
}
|
||||
|
||||
/**
|
||||
* Map Runnable object to certain index of the thread pool.
|
||||
* @param index of the runnable
|
||||
* @param runnable
|
||||
* @param prefix Will name each thread like "Foo-1", "Foo-2", etc.
|
||||
*/
|
||||
fun map(index: Int, runnable: Runnable, prefix: String) {
|
||||
pool[index] = Thread(runnable, "$prefix-$index")
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the thread pool with NULL value.
|
||||
*/
|
||||
fun purge() {
|
||||
for (i in 0..POOL_SIZE)
|
||||
pool[i] = null
|
||||
}
|
||||
|
||||
/**
|
||||
* Start all thread in the pool. If the thread in the pool is NULL, it will simply ignored.
|
||||
*/
|
||||
fun startAll() {
|
||||
pool.forEach { it?.start() }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user