java中的sleep和wait的区别

Python012

java中的sleep和wait的区别,第1张

sleep和wait的区别:

1、sleep的意思是:睡,睡觉,睡眠。

2、wait的意思是:等候,推迟,延缓等待,耽搁,伺候用餐。

拓展资料

sleep的用法

1、They were exhausted from lack of sleep

由于缺乏睡眠,他们非常疲惫。

2、During the car journey, the baby slept

坐车来的路上,宝宝睡着了。

3、I think he may be ready for a sleep soon.

我想他也许很快就要睡一觉了。

4、I can't get to sleep with all that singing.

那些歌声搅得我无法入睡。

5、I didn't lose too much sleep over that investigation.

我并不太担心那个调查。

wait

1、I walk to a street corner and wait for the school bus

我走到街角等校车。

2、There'll be a car waiting for you

会有辆汽车等你。

3、I want to talk to you, but it can wait

我想和你谈谈,但可以晚点再说。

4、If you think this all sounds very exciting, just wait until you read the book

如果你觉得所有这些听起来令人兴奋,那就等着去读这本书吧。

5、'Wait a minute!' he broke in. 'This is not giving her a fair hearing!'

“等一下,”他插嘴说,“这没有给她一个公平的解释机会!”

首先,sleep()是Thread类中的方法,而wait()则是Object类中的方法。

sleep()方法导致了程序暂停,但是他的监控状态依然保持着,当指定的时间到了又会自动恢复运行状态。在调用sleep()方法的过程中,线程不会释放对象锁。

wait()方法会导致线程放弃对象锁,进入等待此对象的等待锁定池,只有针对此对象调用notify()方法后本线程才进入对象锁定池准备获取对象锁进入运行状态。

以下以代码为例讲解:

package test

public class WaitAndSleep {

public static void main(String[] args) {

new Thread(new Thread1()).start()

try {

Thread.sleep(5000)

} catch (Exception e) {

e.printStackTrace()

}

new Thread(new Thread2()).start()

}

private static class Thread1 implements Runnable {

@Override

public void run() {

synchronized (WaitAndSleep.class) {

System.out.println("thread1 is waiting...")

try {

// 调用wait()方法,线程会放弃对象锁,进入等待此对象的等待锁定池

WaitAndSleep.class.wait()

} catch (Exception e) {

e.printStackTrace()

}

System.out.println("thread1 is going on ....")

System.out.println("thread1 is over!!!")

}

}

}

private static class Thread2 implements Runnable {

@Override

public void run() {

synchronized (WaitAndSleep.class) {

System.out.println("enter thread2....")

System.out.println("thread2 is sleep....")

// 只有针对此对象调用notify()方法后本线程才进入对象锁定池准备获取对象锁进入运行状态。

WaitAndSleep.class.notify()

// ==================

// 区别

// 如果我们把代码:TestD.class.notify()给注释掉,即TestD.class调用了wait()方法,但是没有调用notify()

// 方法,则线程永远处于挂起状态。

try {

// sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,

// 但是他的监控状态依然保持者,当指定的时间到了又会自动恢复运行状态。

// 在调用sleep()方法的过程中,线程不会释放对象锁。

Thread.sleep(5000)

} catch (Exception e) {

e.printStackTrace()

}

System.out.println("thread2 is going on....")

System.out.println("thread2 is over!!!")

}

}

}

}

运行结果:

thread1 is waiting...

enter thread2....

thread2 is sleep....

thread2 is going on....

thread2 is over!!!

thread1 is going on ....

thread1 is over!!!

如果我们把代码:WaitAndSleep.class.notify()给注释掉

运行结果:

thread1 is waiting...

enter thread2....

thread2 is sleep....

thread2 is going on....

thread2 is over!!!

程序一直处于挂起状态,无法结束

Java中sleep和wait的区别

① 这两个方法来自不同的类分别是,sleep来自Thread类,和wait来自Object类。

sleep是Thread的静态类方法,谁调用的谁去睡觉,即使在a线程里调用b的sleep方法,实际上还是a去睡觉,要让b线程睡觉要在b的代码中调用sleep。

② 锁: 最主要是sleep方法没有释放锁,而wait方法释放了锁,使得其他线程可以使用同步控制块或者方法。

sleep不出让系统资源;wait是进入线程等待池等待,出让系统资源,其他线程可以占用CPU。一般wait不会加时间限制,因为如果wait线程的运行资源不够,再出来也没用,要等待其他线程调用notify/notifyAll唤醒等待池中的所有线程,才会进入就绪队列等待OS分配系统资源。sleep(milliseconds)可以用时间指定使它自动唤醒过来,如果时间不到只能调用interrupt()强行打断。

Thread.sleep(0)的作用是“触发操作系统立刻重新进行一次CPU竞争”。

③ 使用范围:wait,notify和notifyAll只能在同步控制方法或者同步控制块里面使用,而sleep可以在任何地方使用。

  synchronized(x){

     x.notify()

    //或者wait()

  }

2.  Java中HashMap和HashTable的区别

① 历史原因: Hashtable是给予陈旧的Dictonary类的,  HashMap是Java1.2引进的Map接口的一个实现

② HashMap允许空的键值对, 而HashTable不允许

③ HashTable同步,而HashMap非同步,效率上比HashTable要高

3. 请简述在异常当中,throw和throws有什么区别

① throw代表动作,表示抛出一个异常的动作;throws代表一种状态,代表方法可能有异常抛出

② throw用在方法实现中,而throws用在方法声明中

③ throw只能用于抛出一种异常,而throws可以抛出多个异常

wait

英 [weɪt]   美 [wet]

vt.&vi.

等待等候(尤指长期地)希望盼望

vi.

准备妥在手边可得到可使用

vt.

推迟,搁置,延缓

第三人称单数: waits 现在分词: waiting 过去式: waited 过去分词: waited

sleep

英 [sli:p]   美 [slip]

vi.&link-v.

睡,睡觉

vi.

睡,睡觉睡眠状态

vt.

为…提供床位提供住宿以睡觉打发日子

n.

睡眠

第三人称单数: sleeps 现在分词: sleeping 过去式: slept 过去分词: slept

希望对你有帮助~