본문 바로가기

언어/Java

ThreadSleep

※ 일시정지 sleep() 스케쥴 조절, 알아서 깨어남
timeout되서 sleep상태에서 깨어나면 run()이 아니라 
Runnable로 갔다가 줄을 서고 기다렸다가 다시 cpu 점유할 상태로 가다보니
cpu 점유할 확률 떨어진다

import java.text.SimpleDateFormat;
import java.util.Date;

public class ThreadSleepTest {

	public static void main(String[] args) {
		System.out.println(Thread.currentThread().getName()); // main thread
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
		
//		Thread t = new Thread(()->{	
		new Thread(()->{	
			for(int i = 0; i<10; i++) {
				Date dt = new Date();
				System.out.println(sdf.format(dt));
				try {
					Thread.sleep(1000);//1초
				} catch (InterruptedException e) {
					e.printStackTrace();
				}	
			}
			
		}).start();
//		t.start(); 위의 2줄 없애고 1줄로 통일.. 
		
	}

}

결과
오버라이딩할 run메소드 내부

'언어 > Java' 카테고리의 다른 글

스레드 이름 부여하기  (0) 2023.08.16
Thread pop, push  (0) 2023.08.16
멀티 스레드  (0) 2023.08.16
성능 향상 스트림  (0) 2023.08.15
기본자료형과 참조형의 차이  (0) 2023.08.10