trying to fix the threadexecutor, at least it will throw error if a job failed

This commit is contained in:
minjaesong
2020-10-21 11:06:35 +09:00
parent d5df9df00d
commit 2930336718
4 changed files with 56 additions and 47 deletions

View File

@@ -23,21 +23,13 @@ class Biomegen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, par
private val genSlices = maxOf(ThreadExecutor.threadCount, world.width / 8)
private var genFutures: Array<Future<*>?> = arrayOfNulls(genSlices)
override var generationStarted: Boolean = false
override val generationDone: Boolean
get() = generationStarted && genFutures.fold(true) { acc, f -> acc and (f?.isDone ?: true) }
private val YHEIGHT_MAGIC = 2800.0 / 3.0
private val YHEIGHT_DIVISOR = 2.0 / 7.0
override fun run() {
generationStarted = true
override fun getDone() {
ThreadExecutor.renew()
(0 until world.width).sliceEvenly(genSlices).mapIndexed { i, xs ->
genFutures[i] = ThreadExecutor.submit {
(0 until world.width).sliceEvenly(genSlices).map { xs ->
ThreadExecutor.submit {
val localJoise = getGenerator(seed, params as BiomegenParams)
for (x in xs) {
for (y in 0 until world.height) {

View File

@@ -20,33 +20,28 @@ class Terragen(world: GameWorld, seed: Long, params: Any) : Gen(world, seed, par
private val genSlices = maxOf(ThreadExecutor.threadCount, world.width / 8)
private var genFutures: Array<Future<*>?> = arrayOfNulls(genSlices)
override var generationStarted: Boolean = false
override val generationDone: Boolean
get() = generationStarted && genFutures.fold(true) { acc, f -> acc and (f?.isDone ?: true) }
private val YHEIGHT_MAGIC = 2800.0 / 3.0
private val YHEIGHT_DIVISOR = 2.0 / 7.0
override fun run() {
generationStarted = true
override fun getDone() {
ThreadExecutor.renew()
(0 until world.width).sliceEvenly(genSlices).mapIndexed { i, xs ->
genFutures[i] = ThreadExecutor.submit {
ThreadExecutor.submit {
val localJoise = getGenerator(seed, params as TerragenParams)
for (x in xs) {
for (y in 0 until world.height) {
val sampleTheta = (x.toDouble() / world.width) * TWO_PI
val sampleOffset = world.width / 8.0
val sampleX = sin(sampleTheta) * sampleOffset + sampleOffset // plus sampleOffset to make only
val sampleZ = cos(sampleTheta) * sampleOffset + sampleOffset // positive points are to be sampled
val sampleY = y - (world.height - YHEIGHT_MAGIC) * YHEIGHT_DIVISOR // Q&D offsetting to make ratio of sky:ground to be constant
// DEBUG NOTE: it is the OFFSET FROM THE IDEAL VALUE (observed land height - (HEIGHT * DIVISOR)) that must be constant
val noise = localJoise.map { it.get(sampleX, sampleY, sampleZ) }
val localLock = java.lang.Object() // in an attempt to fix the "premature exit" issue of a thread run
synchronized(localLock) { // also see: https://stackoverflow.com/questions/28818494/threads-stopping-prematurely-for-certain-values
for (x in xs) {
for (y in 0 until world.height) {
val sampleTheta = (x.toDouble() / world.width) * TWO_PI
val sampleOffset = world.width / 8.0
val sampleX = sin(sampleTheta) * sampleOffset + sampleOffset // plus sampleOffset to make only
val sampleZ = cos(sampleTheta) * sampleOffset + sampleOffset // positive points are to be sampled
val sampleY = y - (world.height - YHEIGHT_MAGIC) * YHEIGHT_DIVISOR // Q&D offsetting to make ratio of sky:ground to be constant
// DEBUG NOTE: it is the OFFSET FROM THE IDEAL VALUE (observed land height - (HEIGHT * DIVISOR)) that must be constant
val noise = localJoise.map { it.get(sampleX, sampleY, sampleZ) }
draw(x, y, noise, world)
draw(x, y, noise, world)
}
}
}
}

View File

@@ -3,6 +3,7 @@ package net.torvald.terrarum.modulebasegame.worldgenerator
import net.torvald.terrarum.AppLoader
import net.torvald.terrarum.AppLoader.printdbg
import net.torvald.terrarum.gameworld.GameWorld
import java.util.concurrent.Callable
/**
* New world generator.
@@ -34,7 +35,7 @@ object Worldgen {
val it = jobs[i]
AppLoader.getLoadScreen().addMessage(it.loadingScreenName)
it.theWork.run()
it.theWork.getDone()
}
printdbg(this, "Generation job finished")
@@ -46,9 +47,7 @@ object Worldgen {
}
abstract class Gen(val world: GameWorld, val seed: Long, val params: Any) {
abstract var generationStarted: Boolean
abstract val generationDone: Boolean
open fun run() { }
open fun getDone() { } // trying to use different name so that it won't be confused with Runnable or Callable
}
data class WorldgenParams(