在本文中,我们将学习Java中的yield()、join()和sleep()方法,以及这三种方法之间的基本区别。首先,我们将看到这三种方法的基本介绍,然后比较这三种方法。
我们可以使用thread类的以下方法之一来防止线程的执行。这三种方法都将用于防止线程执行。
1.yield()方法
假设有三个线程t1、t2和t3。线程t1获取处理器并开始执行,线程t2和t3处于就绪/可运行状态。线程t1的完成时间为5小时,t2的完成时间为5分钟。由于t1将在5小时后完成其执行,t2必须等待5小时才能完成5分钟的工作。在这种情况下,一个线程需要花费太多的时间来完成其执行,我们需要一种方法来防止在重要的事情悬而未决时执行中间的线程。收益率()帮助我们做到这一点。
这个 收益率 基本上意味着线程没有做任何特别重要的事情,如果需要运行任何其他线程或进程,它们应该运行。否则,当前线程将继续运行。
收益率法的使用:
- 每当线程调用 JAVA朗。丝线。产量 方法向线程调度程序提示它已准备好暂停其执行。线程调度程序可以随意忽略此提示。
- 如果有任何线程执行yield方法,线程调度程序将检查是否有与该线程具有相同或高优先级的线程。如果处理器发现任何优先级更高或相同的线程,它会将当前线程移动到就绪/可运行状态,并将处理器交给另一个线程,如果没有,则当前线程将继续执行。
- 一旦一个线程执行了yield方法,并且有许多具有相同优先级的线程在等待处理器,那么我们就无法指定哪个线程将首先获得执行机会。
- 执行yield方法的线程将从Running状态进入Runnable状态。
- 一旦一个线程暂停执行,我们就无法指定它何时会再次有机会,这取决于线程调度程序。
- 如果我们使用收益率方法,底层平台必须为抢占式调度提供支持。
2.sleep()方法
此方法会使当前执行的线程休眠指定的毫秒数,具体取决于系统计时器和调度程序的精度和准确性。
语法:
// sleep for the specified number of milliseconds public static void sleep(long millis) throws InterruptedException //sleep for the specified number of milliseconds plus nano seconds public static void sleep(long millis, int nanos) throws InterruptedException
JAVA
// Java program to illustrate // sleep() method in Java import java.lang.*; public class SleepDemo implements Runnable { Thread t; public void run() { for ( int i = 0 ; i < 4 ; i++) { System.out.println( Thread.currentThread().getName() + " " + i); try { // thread to sleep for 1000 milliseconds Thread.sleep( 1000 ); } catch (Exception e) { System.out.println(e); } } } public static void main(String[] args) throws Exception { Thread t = new Thread( new SleepDemo()); // call run() function t.start(); Thread t2 = new Thread( new SleepDemo()); // call run() function t2.start(); } } |
Thread-1 0 Thread-0 0 Thread-0 1 Thread-1 1 Thread-0 2 Thread-1 2 Thread-1 3 Thread-0 3
注:
- 根据要求,我们可以使线程在指定的时间段内处于休眠状态
- Sleep()使线程在给定的时间内完全停止执行;如果不需要运行其他线程或进程,CPU将处于空闲状态(可能会进入节能模式)。
3.join()方法
线程实例的join()方法用于将一个线程执行的开始连接到另一个线程执行的结束,这样一个线程直到另一个线程结束才开始运行。如果对线程实例调用join(),当前运行的线程将阻塞,直到线程实例完成执行。join()方法最多只能等待这么多毫秒,线程才会死亡。超时0表示永远等待
语法:
// waits for this thread to die. public final void join() throws InterruptedException // waits at most this much milliseconds for this thread to die public final void join(long millis) throws InterruptedException // waits at most milliseconds plus nanoseconds for this thread to die. The java.lang.Thread.join(long millis, int nanos)
JAVA
// Java program to illustrate join() method in Java import java.lang.*; public class JoinDemo implements Runnable { public void run() { Thread t = Thread.currentThread(); System.out.println( "Current thread: " + t.getName()); // checks if current thread is alive System.out.println( "Is Alive? " + t.isAlive()); } public static void main(String args[]) throws Exception { Thread t = new Thread( new JoinDemo()); t.start(); // Waits for 1000ms this thread to die. t.join( 1000 ); System.out.println( "Joining after 1000" + " milliseconds: " ); System.out.println( "Current thread: " + t.getName()); // Checks if this thread is alive System.out.println( "Is alive? " + t.isAlive()); } } |
Current thread: Thread-0 Is Alive? true Joining after 1000 milliseconds: Current thread: Thread-0 Is alive? false
注:
- 如果任何执行线程t1在t2上调用join(),即:;t2。join()立即t1将进入等待状态,直到t2完成执行。
- 在join()中给定一个超时,将使join()效果在特定超时后为空。
yield()、join()和sleep()方法的比较
所有物 | 收益率 | 加入 | 睡眠() |
---|---|---|---|
意图 | 如果一个线程想要通过它的执行来给其他具有相同优先级的线程提供机会,那么我们应该选择yield() | 如果一个线程想要等待其他线程的完成,那么我们应该选择join() | 如果一个线程在特定的时间内不想执行任何操作,那么它将进入sleep() |
超载了吗? | 不 | 对 | 对 |
这是最终结果吗? | 不 | 对 | 不 |
它会扔吗? | 不 | 对 | 对 |
是土生土长的吗? | 对 | 不 | 睡眠(长毫秒)->本机和睡眠(长毫秒,int-ns)->非本机 |
是静电的吗? | 对 | 不 | 对 |
本文由 达尔梅什 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。