티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

How to check if current thread is not main thread

현재 스레드가 메인 스레드(main thread)가 아닌지 확인하는 방법

 문제 내용 

I need to check if the thread running a certain piece of code is the main (UI) thread or not. How can I achieve this?

특정 코드를 실행 중인 스레드가 메인(UI) 스레드인지 여부를 확인해야합니다. 이를 어떻게 확인할 수 있을까요?

 

 

 

 높은 점수를 받은 Solution 

Looper.myLooper() == Looper.getMainLooper()

if this returns true, then you're on the UI thread!

만약 이것이 참을 반환하면, 당신은 UI 스레드에 있습니다!

 

 

 

 가장 최근 달린 Solution 

First of all check It is main Thread or not

우선, 이것이 메인 스레드인지 확인하세요.

 

In Kotlin

fun isRunningOnMainThread(): Boolean {
    return Thread.currentThread() == Looper.getMainLooper().thread
}

 

In Java

static boolean isRunningOnMainThread() {
  return Thread.currentThread().equals(Looper.getMainLooper().getThread());
}

 

 

출처 : https://stackoverflow.com/questions/11411022/how-to-check-if-current-thread-is-not-main-thread

반응형
댓글
공지사항
최근에 올라온 글