Created
April 4, 2020 15:07
-
-
Save charlesmuchene/7ee25c4d2c3b4e7f3e84ab74784b99f2 to your computer and use it in GitHub Desktop.
Handler Thread usage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class NQueensActivity : AppCompatActivity() { | |
private val handlerThread = HandlerThread("NQueens").also { it.start() } | |
private val handler = Handler(handlerThread.looper) | |
private var queens = 1 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
solveButton.setOnClickListener { | |
solveNQueens(queens) | |
} | |
} | |
private fun solveNQueens(queens: Int) { | |
handler.post { | |
val positions = ... // Calculate possible queen positions | |
Handler(mainLooper).post { | |
renderUI(positions) | |
} | |
} | |
} | |
override fun onStop() { | |
super.onStop() | |
handlerThread.quitSafely() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment