feat: add variable num of query threads

This commit is contained in:
Assasinnys 2020-05-12 19:48:24 +03:00
parent e05b23db9f
commit 0874eff533

View File

@ -30,13 +30,14 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
private val closedList = mutableListOf<StarPoint>() private val closedList = mutableListOf<StarPoint>()
private val stopwatch = Stopwatch() private val stopwatch = Stopwatch()
private val threadPool = Executors.newFixedThreadPool(2) private val threadPool = Executors.newFixedThreadPool(NUM_OF_THREADS)
private val dbList = mutableListOf<Database>()
val db1 = Database().apply { openConnection() }
val db2 = Database().apply { openConnection() }
init { init {
openedList.add(startStarPoint) openedList.add(startStarPoint)
repeat(NUM_OF_THREADS) {
dbList.add(Database().apply { openConnection() })
}
// println("startStarPoint=${startStarPoint.systemId64}") // println("startStarPoint=${startStarPoint.systemId64}")
// println("finishStarPoint=${finishStarPoint.systemId64}") // println("finishStarPoint=${finishStarPoint.systemId64}")
} }
@ -96,36 +97,36 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
} }
private fun multithreatingFindNeighbours(starPoint: StarPoint) { private fun multithreatingFindNeighbours(starPoint: StarPoint) {
var firstRange = 0 to 30 val range = if (starPoint.isNeutronStar)
var secondRange = 30 to 60 NEUTRON_DISTANCE.div(NUM_OF_THREADS)
if (starPoint.isNeutronStar) { else
firstRange = 0 to 120 USUAL_DISTANCE.div(NUM_OF_THREADS)
secondRange = 120 to 240 val sqlList = mutableListOf<String>()
}
val sql1 = "select $C_ID64, $C_X, $C_Y, $C_Z, $C_SUBTYPE = 'Neutron Star' as isNeutronStar, " +
"$C_SYS_NAME, " +
"sqrt((${starPoint.coords.x}-x)^2+(${starPoint.coords.y}-y)^2+(${starPoint.coords.z}-z)^2) as dist\n" +
"from $CORRIDOR\n" +
"where sqrt((${starPoint.coords.x}-x)^2+(${starPoint.coords.y}-y)^2+(${starPoint.coords.z}-z)^2) between ${firstRange.first} and ${firstRange.second}" +
"and not $C_ID64=${starPoint.systemId64}"
val sql2 = "select $C_ID64, $C_X, $C_Y, $C_Z, $C_SUBTYPE = 'Neutron Star' as isNeutronStar, " +
"$C_SYS_NAME, " +
"sqrt((${starPoint.coords.x}-x)^2+(${starPoint.coords.y}-y)^2+(${starPoint.coords.z}-z)^2) as dist\n" +
"from $CORRIDOR\n" +
"where sqrt((${starPoint.coords.x}-x)^2+(${starPoint.coords.y}-y)^2+(${starPoint.coords.z}-z)^2) between ${secondRange.first} and ${secondRange.second}" +
"and not $C_ID64=${starPoint.systemId64}"
val taskList = mutableListOf<Future<*>>() val taskList = mutableListOf<Future<*>>()
taskList.add( val maxJump = if (starPoint.isNeutronStar)
threadPool.submit { NEUTRON_DISTANCE
findNeighbours(starPoint, sql1, db1) else
} USUAL_DISTANCE
)
taskList.add( for (i in 0 until maxJump step range) {
threadPool.submit { val sql = "select $C_ID64, $C_X, $C_Y, $C_Z, $C_SUBTYPE = 'Neutron Star' as isNeutronStar, " +
findNeighbours(starPoint, sql2, db2) "$C_SYS_NAME, " +
} "sqrt((${starPoint.coords.x}-x)^2+(${starPoint.coords.y}-y)^2+(${starPoint.coords.z}-z)^2) as dist\n" +
) "from $CORRIDOR\n" +
"where sqrt((${starPoint.coords.x}-x)^2+(${starPoint.coords.y}-y)^2+(${starPoint.coords.z}-z)^2) between $i and ${i.plus(
range
)}" +
"and not $C_ID64=${starPoint.systemId64}"
sqlList.add(sql)
}
for (i in 0 until NUM_OF_THREADS) {
taskList.add(
threadPool.submit {
findNeighbours(starPoint, sqlList[i], dbList[i])
}
)
}
taskList.forEach { taskList.forEach {
it.get() it.get()
@ -272,8 +273,9 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
counter++ counter++
} }
println("${consoleStringCounter()} Total jumps counter = $counter, distance = $fullDistance ly, replaces = $replaces, cof = ${StarPoint.NEUTRON_COF}") println("${consoleStringCounter()} Total jumps counter = $counter, distance = $fullDistance ly, replaces = $replaces, cof = ${StarPoint.NEUTRON_COF}")
db1.closeDB() dbList.forEach { db ->
db2.closeDB() db.closeDB()
}
return counter return counter
} }
@ -287,5 +289,6 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin
const val CORRIDOR = "main" const val CORRIDOR = "main"
const val NEUTRON_DISTANCE = 240 const val NEUTRON_DISTANCE = 240
const val USUAL_DISTANCE = 60 const val USUAL_DISTANCE = 60
const val NUM_OF_THREADS = 3
} }
} }