언어/Java
ThreadSleep
sector
2023. 8. 16. 00:54

※ 일시정지 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줄로 통일..
}
}

