You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
스레드 시작1
1. 자바 메모리 구조 복습
1) 메서드 영역(Method Area)
2) 스택 영역(Stack Area)
3) 힙 영역(Heap Area)
2. 스레드 생성
Thread클래스를 상속받거나Runnable인터페이스를 구현하여 스레드를 만들 수 있다.Thread클래스를 상속받고run()메서드를 재정의하여 스레드가 수행할 작업을 정의한다.Thread.currentThread().getName()은 현재 실행 중인 스레드의 이름을 반환한다.start()메서드는 새로운 스레드를 시작하는 메서드이다.run()메서드를 직접 호출하면 새로운 스레드가 생성되지 않고, 단순히 main 스레드에서 실행된다.helloThread.start()를 호출하면 자바는 스레드를 위한 별도의 스택 공간을 할당하고run()메서드를 실행시킨다.start()메서드를 호출하면 main 스레드는 멈추지 않고 계속 실행된다.run()메서드를 실행하도록 요청할 뿐이며, 실제 실행 순서는 CPU 스케줄러에 의해 결정된다.3. 스레드 특징
1) 스레드 간의 실행 순서는 보장하지 않는다.
스레드 시작2
1. start() vs run()
helloThread.start()메서드 대신helloThread.run()메서드를 직접 호출한 예시이다.데몬 스레드
1. 스레드의 종류
1) 사용자 스레드
2) 데몬 스레드
setDaemon(true)메서드는 스레드를 데몬 스레드로 지정한다.start()메서드 호출 전에 지정해야 하며, 그렇지 않으면IllegalThreadStateException이 발생한다.DaemonThread는 데몬 스레드로 설정되어 있으므로, main 스레드(유일한 사용자 스레드)가 종료되는 순간 JVM이 함께 종료된다.run() end메시지는 출력되지 않는다.run() end메세지가 출력되기 전에 프로그램이 종료된다.Runnable
1. Runnable
run()을 가지고 있으며, 이를 반드시 구현해야 한다.Runnable인터페이스를 구현하여run()메서드 내부에 실행할 작업을 정의한 것이다.Runnable을 구현한 객체를Thread생성자의 인자로 전달하여 스레드를 실행한다.Thread클래스를 직접 상속받은 경우와 동일하게 동작한다.2. Thread 상속 vs Runnable 구현
Thread클래스를 직접 상속받는 것보다Runnable인터페이스를 구현하는 방식을 권장한다.1) Thread 상속
Thread클래스를 상속받아run()메서드만 재정의하면 되므로 구현이 간단하다.Thread클래스를 상속받을 수 없다.2) Runnable 구현
Runnable을 구현하여 문제없이 스레드를 정의할 수 있다.Thread)와 실행할 작업(Runnable)이 분리되어 있어 코드의 가독성과 재사용성이 높다.Runnable객체를 여러 스레드에서 공유할 수 있으므로 자원 관리가 효율적이다.Runnable객체를 생성하고, 이를Thread생성자에 전달하는 추가 과정이 필요하므로 코드가 약간 복잡해진다.All reactions