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()
的具体作用了。