sleep与wait

看到别人提到的有面试里问了这个,于是去大概百度了一下。

总体来说这俩表面上的效果差不多,都是使当前线程让出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没指定时间),像这样:
在这里插入图片描述
这个时候可能就需要使用一些其他的线程中断的手段来结束这个线程了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值