본문 바로가기

Dead Code/DEPRECATED-KOTLIN

[코틀린코드연습장] 쓰레드...(4) RunOnUIThread


사용자가 만든 쓰레드 내의 화면처리를 메인쓰레드로 옮겨서 적용하는 방법 중.

가장 간단한 방법인가보다.

역시, 간단한 방법이 있었구나, 


발생시킨 쓰레드 클래스 안에 런온유아이쓰레드 람다식을 쓰면 된다.



class MainActivity : AppCompatActivity() {

var isRunning = true

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

button2.setOnClickListener {
var time = System.currentTimeMillis()
textView3.text = "button click : ${time}"
}

isRunning = true

var timethread = TimeThread()
timethread.start()


}

override fun onDestroy() {
super.onDestroy()
isRunning = false
}

inner class TimeThread : Thread() {
override fun run() {
while (isRunning) {
SystemClock.sleep(100)
var time = System.currentTimeMillis()
Log.d("timeThread", "thread : ${time}")

runOnUiThread {
textView4.text = "RunOnUIThread : ${time}"
}
}
}
}
}