From 0874eff53390ab6f2e37d13f4e38771253a8164b Mon Sep 17 00:00:00 2001 From: Assasinnys Date: Tue, 12 May 2020 19:48:24 +0300 Subject: [PATCH] feat: add variable num of query threads --- src/main/kotlin/elite/algorithm/AStarMain.kt | 73 ++++++++++---------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/src/main/kotlin/elite/algorithm/AStarMain.kt b/src/main/kotlin/elite/algorithm/AStarMain.kt index 79272a1..76cebd3 100644 --- a/src/main/kotlin/elite/algorithm/AStarMain.kt +++ b/src/main/kotlin/elite/algorithm/AStarMain.kt @@ -30,13 +30,14 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin private val closedList = mutableListOf() private val stopwatch = Stopwatch() - private val threadPool = Executors.newFixedThreadPool(2) - - val db1 = Database().apply { openConnection() } - val db2 = Database().apply { openConnection() } + private val threadPool = Executors.newFixedThreadPool(NUM_OF_THREADS) + private val dbList = mutableListOf() init { openedList.add(startStarPoint) + repeat(NUM_OF_THREADS) { + dbList.add(Database().apply { openConnection() }) + } // println("startStarPoint=${startStarPoint.systemId64}") // println("finishStarPoint=${finishStarPoint.systemId64}") } @@ -96,36 +97,36 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin } private fun multithreatingFindNeighbours(starPoint: StarPoint) { - var firstRange = 0 to 30 - var secondRange = 30 to 60 - if (starPoint.isNeutronStar) { - firstRange = 0 to 120 - secondRange = 120 to 240 - } - 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 range = if (starPoint.isNeutronStar) + NEUTRON_DISTANCE.div(NUM_OF_THREADS) + else + USUAL_DISTANCE.div(NUM_OF_THREADS) + val sqlList = mutableListOf() val taskList = mutableListOf>() - taskList.add( - threadPool.submit { - findNeighbours(starPoint, sql1, db1) - } - ) - taskList.add( - threadPool.submit { - findNeighbours(starPoint, sql2, db2) - } - ) + val maxJump = if (starPoint.isNeutronStar) + NEUTRON_DISTANCE + else + USUAL_DISTANCE + + for (i in 0 until maxJump step range) { + val sql = "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 $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 { it.get() @@ -272,8 +273,9 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin counter++ } println("${consoleStringCounter()} Total jumps counter = $counter, distance = $fullDistance ly, replaces = $replaces, cof = ${StarPoint.NEUTRON_COF}") - db1.closeDB() - db2.closeDB() + dbList.forEach { db -> + db.closeDB() + } return counter } @@ -287,5 +289,6 @@ class AStarMain(private val startSystem: String, private val finishSystem: Strin const val CORRIDOR = "main" const val NEUTRON_DISTANCE = 240 const val USUAL_DISTANCE = 60 + const val NUM_OF_THREADS = 3 } } \ No newline at end of file