看到别人提到的有面试里问了这个,于是去大概百度了一下。
总体来说这俩表面上的效果差不多,都是使当前线程让出cpu的方法。
sleep是Thread类的静态方法,而wait是Object的方法。
wait一般是配合notify,notifyAll这些方法在同步块中使用,sleep使用范围广一些,非同步块的代码也会有用到(我这里曾经在爬虫里大量使用sleep来实现延时)。
在同步方法里使用的时候,sleep是不会释放对象锁的,而wait则会释放对象锁,如果wait方法里没有给定具体的等待时间,那么需要有其他线程使用notify这类的方法去唤醒它,不然就会一直卡住了。
写(抄)了俩简单小例子:
public class SleepTestClass1 implements Runnable{
@Override
public void run() {
synchronized (SleepTestClass1.class) {
System.out.println("线程"+Thread.currentThread().getName()+"启动.........");
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("线程"+Thread.currentThread().getName()+"休眠完毕,继续执行");
System.out.println("线程"+Thread.currentThread().getName()+"执行结束");
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new SleepTestClass1(), "线程t1");
Thread t2 = new Thread(new SleepTestClass1(), "线程t2");
t1.start();
t2.start();
}
}
运行结果:
虽然t1进入休眠状态,但是因为sleep不释放对象锁,所以t1仍然持有锁,那么其他线程就无法执行,只能等待t1休眠结束后继续执行完毕才能获得锁。
如果把上面代码里synchronized关键字的部分注掉,再次运行:
没有了同步锁,在线程t1进入休眠后,线程t2就立刻抢占cpu开始执行了。
关于wait部分的代码:
public class SleepTestClass2 implements Runnable{
@Override
public void run() {
synchronized (SleepTestClass2.class) {
System.out.println("线程"+Thread.currentThread().getName()+"启动.........");
System.out.println("线程"+Thread.currentThread().getName()+"执行中...............");
System.out.println("线程"+Thread.currentThread().getName()+"执行结束");
SleepTestClass2.class.notify();
}
}
public static void main(String[] args) {
Thread t1 = new Thread(new SleepTestClass2(), "线程t1");
Thread t2 = new Thread(new WaitThread(), "线程t2");
t2.start();
t1.start();
}
}
class WaitThread implements Runnable{
@Override
public void run() {
synchronized (SleepTestClass2.class) {
System.out.println("线程"+Thread.currentThread().getName()+"启动.........");
try {
SleepTestClass2.class.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("线程"+Thread.currentThread().getName()+"等待结束...............");
System.out.println("线程"+Thread.currentThread().getName()+"执行结束");
}
}
}
运行结果:
这里是线程t2先运行,但是进入wait状态后释放了对象锁,于是线程t1开始运行,t1结束后调用notify唤醒t2继续运行到结束。
如果t1没有调用notify,t2就会一直卡住在wait的状态里(因为wait没指定时间),像这样:
这个时候可能就需要使用一些其他的线程中断的手段来结束这个线程了。