logo

예제가 포함된 Java의 Thread.sleep() 메서드

스레드 클래스는 기본적으로 프로그램 실행 스레드인 클래스입니다. Java.lang 패키지에 있습니다. 스레드 클래스에는 다음이 포함됩니다. 잠() 방법. Thread Class에는 Sleep() 메서드의 오버로드된 메서드가 두 개 있는데, 하나는 인수가 하나이고 다른 하나는 인수가 두 개입니다. sleep() 메소드는 특정 시간 동안 현재 스레드(시스템에서 실행 중일 수 있는 스레드)의 실행을 중지하는 데 사용되며 해당 시간이 지나면 이전에 실행 중인 스레드가 다시 실행되기 시작합니다.

Thread.sleep() 메소드에 관한 중요한 사항:



  • 메서드 Thread.sleep() 함수가 실행될 때마다 항상 현재 스레드 실행을 일시 중지합니다.
  • 스레드가 휴면 상태일 때 다른 스레드가 인터럽트하면 InterruptedException이 발생합니다.
  • 시스템이 사용 중인 경우 스레드가 절전 모드로 전환되는 실제 시간은 절전 메서드를 호출하는 동안 전달된 시간보다 길며, 시스템 로드가 적으면 스레드의 실제 절전 시간은 호출하는 동안 경과된 시간에 가깝습니다. sleep() 메소드를 호출합니다.

Sleep() 메소드의 구문

Java Thread에는 sleep() 메소드의 4가지 변형이 있습니다. 이것들은:

 1.  public static void sleep(long millis)throws InterruptedException 2.  public static void sleep(long millis)throws IllegalArguementException 3.  public static void sleep(long millis, int nanos)throws InterruptedException 4.  public static void sleep(long millis, int nanos)throws IllegalArguementException>

Thread.Sleep() 메서드에 전달된 매개 변수



    millis: 스레드가 절전 모드로 전환되는 시간(밀리초) nanos: 스레드가 절전 모드로 전환되기를 원하는 추가 시간(나노초)입니다. 범위는 0~999999입니다.

Sleep() 메소드의 반환 유형: 어떤 값도 반환하지 않습니다. 즉, sleep 함수의 반환 유형은 void입니다.

하나의 매개변수가 있는 sleep 메소드는 기본 메소드입니다. 즉, 이 메소드의 구현은 다른 프로그래밍 언어로 수행되고 두 개의 매개변수가 있는 다른 메소드는 원시 메소드가 아닙니다. 즉, 해당 구현은 Java로 수행됩니다. 두 가지 sleep 메소드 모두 정적입니다. 즉, Thread 클래스를 사용하여 접근할 수 있습니다. 두 메소드 모두 확인된 예외를 발생시킵니다. 즉, throws 키워드를 사용하거나 try 및 catch 블록을 사용하여 예외를 처리할 수 있습니다.

모든 스레드에 대해 Thread.Sleep() 메서드를 사용할 수 있습니다. 즉, 메인 스레드나 프로그래밍 방식으로 만드는 다른 스레드에서 이를 수행할 수 있습니다.

1. 메인 스레드에 Thread.Sleep() 메서드 사용

자바




// Java Program for sleeping the main thread.> import> java.io.*;> import> java.lang.Thread;> class> GFG {> >public> static> void> main(String[] args)> >{> >// we can also use throws keyword followed by> >// exception name for throwing the exception> > >try> {> >for> (>int> i =>0>; i <>5>; i++) {> > >// it will sleep the main thread for 1 sec> >// ,each time the for loop runs> >Thread.sleep(>1000>);> > >// printing the value of the variable> >System.out.println(i);> >}> >}> >catch> (Exception e) {> > >// catching the exception> >System.out.println(e);> >}> >}> }>

>

>

산출

0 1 2 3 4>

2. 사용자 정의 스레드에 Thread.Sleep() 메서드 사용

자바


안드로이드에서 차단된 번호를 확인하는 방법



// Java Program for sleeping the custom thread.> import> java.io.*;> import> java.lang.Thread;> class> GFG>extends> Thread {> >public> void> run()> >{> >// thread 0> >// we can also use throws keyword followed by> >// exception name for throwing the exception> >try> {> >for> (>int> i =>0>; i <>5>; i++) {> > >// it will sleep the main thread for 1 sec> >// ,each time the for loop runs> >Thread.sleep(>1000>);> > >// This Thread.sleep() method will sleep the> >// thread 0.> >// printing the value of the variable> >System.out.println(i);> >}> >}> >catch> (Exception e) {> > >// catching the exception> >System.out.println(e);> >}> >}> >public> static> void> main(String[] args)> >{> >// main thread> >GFG obj =>new> GFG();> >obj.start();> >}> }>

>

>

산출

0 1 2 3 4>

3. 수면 시간이 음수인 경우 IllegalArguementException

자바




// Java Program for showing how exception can occur if we> // pass the negative timeout value.> import> java.io.*;> import> java.lang.Thread;> class> GFG {> >public> static> void> main(String[] args)> >{> >// we can also use throws keyword followed by> >// exception name for throwing the exception> > >try> {> >for> (>int> i =>0>; i <>5>; i++) {> > >// this will throw the> >// IllegalArgumentException> >Thread.sleep(->100>);> > >// printing the value of the variable> >System.out.println(i);> >}> >}> >catch> (Exception e) {> > >// catching the exception> >System.out.println(e);> >}> >}> }>

>

>

산출

java.lang.IllegalArgumentException: timeout value is negative>