📜  Java中的 Thread.sleep() 方法和示例

📅  最后修改于: 2022-05-13 01:54:50.760000             🧑  作者: Mango

Java中的 Thread.sleep() 方法和示例

线程类是一个基本上是程序执行线程的类。它存在于Java.lang 包中。 Thread 类包含Sleep()方法。线程类中存在两种 Sleep() 方法的重载方法,一种带有一个参数,另一种带有两个参数。 sleep() 方法用于在特定的时间段内停止当前线程(系统中可能正在执行的线程)的执行,在该时间段结束后,较早执行的线程将再次开始执行。

关于 Thread.sleep() 方法的要点:

  • 方法 每当 Thread.sleep() 函数执行时,它总是暂停当前线程的执行。
  • 如果任何其他线程在线程休眠时中断,则将抛出 InterruptedException。
  • 如果系统繁忙,那么线程实际休眠的时间会调用sleep 方法时经过的时间多,如果系统负载较少,那么线程的实际休眠时间将接近于调用 sleep 方法时经过的时间调用 sleep() 方法。

Sleep() 方法的语法

  • public static void sleep(long millis)throws InterruptedException
  • public static void sleep(long millis)throws IllegalArgumentException
  • public static void sleep(long millis, int nanos)throws InterruptedException
  • public static void sleep(long millis, int nanos)throws IllegalArgumentException

Thread.Sleep() 方法中传递的参数

  • 毫秒:线程将休眠的持续时间(以毫秒为单位)
  • nanos:这是我们希望线程休眠的额外时间(以纳秒为单位)它的范围从 0 到 999999。

Sleep()方法的返回类型:它不返回任何值,睡眠函数的返回类型为void。

带一个参数的 sleep 方法是本地方法,即该方法的实现是用另一种编程语言完成的,而另一个带两个参数的方法不是本地方法,即它的实现是用Java完成的。这两个 sleep 方法都是静态的,即我们可以使用 Thread 类访问它们。这两种方法都会抛出一个已检查的异常,即我们可以通过使用 throws 关键字或使用 try 和 catch 块来处理异常。

我们可以对任何线程使用 Thread.Sleep() 方法,即我们可以在主线程或我们以编程方式创建的任何其他线程中使用它。

  • 对主线程使用 Thread.Sleep() 方法
Java
// 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 foloowed 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);
        }
    }
}


Java
// 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 foloowed 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();
    }
}


Java
// 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 foloowed 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);
        }
    }
}


输出
0
1
2
3
4
  • 使用 Thread.Sleep() 方法自定义线程

Java

// 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 foloowed 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
  • 睡眠时间为负时的 IllegalArgumentException

Java

// 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 foloowed 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