상세 컨텐츠

본문 제목

Thread

Android(강의정리)

by 낙지지 2020. 11. 18. 15:38

본문

Thread(쓰레드)
- 작업 흐름

앱실행 -----> launcher Activity -----> ----> ------> 작업흐름


안드로이드의 쓰레드
-> MainThread
-------------------------------------------------------------->
-> launcher Activity
-> Activity B
-> 영상 재생
-> 기타등등


할일 : 더하기, 빼기, 곱하기, 나누기
MainThread만 있는 경우
-------------------------------------------------------------->
---------> 더하기 ---------> 빼기 ---------> 곱하기 ---------> 나누기


다른 쓰레드가 있는 경우 -> 여러가지 일을 한번에 할 수 있다
-------------------------------------------------------------->
빼기
------------------>
곱하기
------------------->
더하기
------------------------------------------------------>
나누기
-------------->

안드로이드 MainThread의 특징
- UI(User Interface) Thread
- 사용자의 input을 받는 쓰레드
- 절대 정지시킬수 없다!!(하면 안된다!)
- 왜냐하면, 정지 시키거나 종료 시키면 더 이상 사용자의 input을 받을수 없기 때문!


package com.example.myapplication

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import kotlinx.android.synthetic.main.activity_thread.*

class ThreadActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_thread)

        val runnable: Runnable = object : Runnable{
            override fun run() {
                Log.d("thread-1", "Thread1 is made")
            }
        }
        val thread: Thread = Thread(runnable)

        button.setOnClickListener {
            thread.start()
        }

        // 바로실행
        Thread(object : Runnable{
            override fun run() {
                Log.d("thread-1", "Thread2 is made")
            }
        }).start()
        // 바로실행 - 람다
        Thread(Runnable {
            Thread.sleep(2000)
            Log.d("thread-1", "Thread3 is made")
            runOnUiThread {
                button.setBackgroundColor(getColor(R.color.textview_color))
            }

        }).start()


    }
}

'Android(강의정리)' 카테고리의 다른 글

addView  (0) 2020.11.18
Library  (0) 2020.11.18
Context  (0) 2020.11.18
NullSafety, lateinit  (0) 2020.11.18
Fragment  (0) 2020.11.18

관련글 더보기

댓글 영역