java中的 sleep()、wait()和yield()的区别

java中的 sleep()、wait()和yield()的区别

1 区别

java中,sleep()yield()方法是Thread类中的方法,而wait()Object类中的方法。也就是说,在java中,所有的类都有wait()方法,而只有继承了Thread类的方法才有sleep()yield()方法。

2 其中sleep()和wait()的区别

  • sleep()方法用于短暂暂停线程的运行,而wait()方法用于线程间的通信

  • 线程调用sleep()方法后,当前线程不会释放其持有的资源;线程调用wait()方法后,当前线程会释放其持有的资源

  • sleep()是一个静态方法,作用在当前线程上;而wait()是一个实例方法,并且只能由其他线程调用本实例的notify()或者notifyAll()方法后才能被唤醒。且如果线程调用sleep()方法被唤醒后,会立刻进入就绪状态,而调用wait()方法的线程则不会立刻进入,而是先获得同步锁,然后才进入就绪状态。
    在这里插入图片描述

  • wait()只能在同步环境中被使用,而sleep()没有限制,可以在任何地方使用

  • wait()方法针对的是一个被同步代码块加锁的对象,而sleep()针对的是一个线程进行休眠。

3 sleep()和yield()的区别

sleep()不同的是,当前线程执行yield()后,也就是告诉CPU,当前线程已经执行的差不多了,线程调度器可以将当前的CPU让给那些比当前线程优先级更高的线程或者优先级和当前线程同级的线程去执行,如果没有这样的线程,那么当前线程继续执行,如果有这样的线程,那么当前线程进入就绪状态。

4 sleep()的注意

  • 执行t.sleep()是不会让线程t暂停,而是当前线程暂停,因为sleep()是一个静态方法。
  • 当前一个线程执行完sleep()方法后,不能保证能立马获得CPU资源,而是需要竞争,也就是当前线程会进入就绪状态。
  • 如果其他线程中断了一个正在休眠的线程(调用sleep()方法休眠的线程),那么sleep()会抛出一个Interrupted Exception异常
  • 一个线程执行完sleep()后,会将其本身拥有的cpu资源让给线程调度器
  • sleep()有两个方法,一个是传入一个毫秒数,一个需要传入一个毫秒数和一个纳秒数
public static native void sleep(long millis) throws InterruptedException;
public static void sleep(long millis, int nanos) throws InterruptedException {}
  • yield()sleep()方法都是作用在当前线程上,因为他们都是静态方法,不是实例方法。
public static native void yield();

5 join()的作用

在多线程中,使用join()方法用于让主线程等待子线程运行完之后,主线程才继续执行。如下面的代码,不加join()方法:

public static void main(String[] args) throws InterruptedException {
      TreeNode root = TreeNode.getBST();
       System.out.println(new FDASF().getMinGap(root));

       Thread[] tt = new Thread[5];
       for (int i = 0; i < 5; i++) {
           tt[i] = new Thread(new Runnable() {
               @Override
               public void run() {
                   for (int j = 0; j < 5; j++) {
                       System.out.println("this is thread ==" + this.getClass().getName() + "  j=" + j);
                   }
               }
           });
       }

       for (int i = 0; i < tt.length; i++) {
           tt[i].start();
       }
   }

运行结果:
this is thread ==Thread-0 j=0
this is thread ==Thread-0 j=1
this is thread ==Thread-2 j=0
this is thread ==Thread-0 j=2
this is thread ==Thread-3 j=0
this is thread ==Thread-3 j=1
this is thread ==Thread-2 j=1
this is thread ==Thread-4 j=0
this is thread ==Thread-1 j=0
this is thread ==Thread-3 j=2
this is thread ==Thread-0 j=3
this is thread ==Thread-3 j=3
this is thread ==Thread-1 j=1
this is thread ==Thread-4 j=1
this is thread ==Thread-2 j=2
this is thread ==Thread-4 j=2
this is thread ==Thread-1 j=2
this is thread ==Thread-1 j=3
this is thread ==Thread-1 j=4
this is thread ==Thread-3 j=4
this is thread ==Thread-0 j=4
this is thread ==Thread-4 j=3
this is thread ==Thread-2 j=3
this is thread ==Thread-4 j=4
this is thread ==Thread-2 j=4

加上join()方法之后:

public static void main(String[] args) throws InterruptedException {
      TreeNode root = TreeNode.getBST();
       System.out.println(new FDASF().getMinGap(root));

       Thread[] tt = new Thread[5];
       for (int i = 0; i < 5; i++) {
           tt[i] = new Thread(new Runnable() {
               @Override
               public void run() {
                   for (int j = 0; j < 5; j++) {
                       System.out.println("this is thread ==" + this.getClass().getName() + "  j=" + j);
                   }
               }
           });
       }

       for (int i = 0; i < tt.length; i++) {
           tt[i].start();
           tt[i].join();
       }
   }

运行结果:
this is thread ==Thread-0 j=0
this is thread ==Thread-0 j=1
this is thread ==Thread-0 j=2
this is thread ==Thread-0 j=3
this is thread ==Thread-0 j=4
this is thread ==Thread-1 j=0
this is thread ==Thread-1 j=1
this is thread ==Thread-1 j=2
this is thread ==Thread-1 j=3
this is thread ==Thread-1 j=4
this is thread ==Thread-2 j=0
this is thread ==Thread-2 j=1
this is thread ==Thread-2 j=2
this is thread ==Thread-2 j=3
this is thread ==Thread-2 j=4
this is thread ==Thread-3 j=0
this is thread ==Thread-3 j=1
this is thread ==Thread-3 j=2
this is thread ==Thread-3 j=3
this is thread ==Thread-3 j=4
this is thread ==Thread-4 j=0
this is thread ==Thread-4 j=1
this is thread ==Thread-4 j=2
this is thread ==Thread-4 j=3
this is thread ==Thread-4 j=4

从上面的代码以及运行结果可以看出,join()的具体作用了。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值