Skip to content

Instantly share code, notes, and snippets.

@featzima
Last active April 10, 2019 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save featzima/64c3c5edabc64af158c3b5a4c4cd16ea to your computer and use it in GitHub Desktop.
Save featzima/64c3c5edabc64af158c3b5a4c4cd16ea to your computer and use it in GitHub Desktop.
class CounterThread : Thread() {
private lateinit var handler: Handler
override fun run() {
Looper.prepare()
handler = object: Handler() {
private var counter = 100
override fun handleMessage(msg: Message) {
counter += msg.arg1
if (msg.arg1 == -1) handler.sendMessageDelayed(Message.obtain(msg), 1000)
Log.e("CounterThread", "$counter")
}
}
handler.sendMessage(Message().apply { arg1 = -1 })
Looper.loop()
}
fun increment20() = handler.sendMessage(Message().apply { arg1 = 20 })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment