sleep与wait有什么区别(What is the difference between sleep and wait)

      sleep()是使线程暂停执行一段时间的方法。wait()也是一种使线程暂停执行的方法,例如,当线程交互时,如果线程对一个同步线程x发出一个wait()调用请求,

那么该线程会暂停执行,被调对象进入等待状态,直到被唤醒或等待时间超时。

      具体而言,sleep与wait的区别主要表现在以下几个方面:

      1)原理不同。sleep是Thread类的静态方法,是线程用来控制自身流程的,它会使此线程暂停执行指定时间,而把执行机会会让给其他线程,等到计时时间到时,

此线程会自动苏醒。例如,当线程执行报时功能时,每一秒钟打印出一个时间,那么此时就需要在打印方法前面加上一个sleep方法,以便让自己每隔一秒执行一次,

该过程如同闹钟一样。而wait是Object类的方法,用于线程间的通信,这个方法会使当前拥有对象锁的进程等待,直到其他线程调用notify方法(或notifyAll方法)

时才醒来。一个开发人员也可以给它指定一个时间,自动醒来。与wait配套的方法有notify和notifyAll。

     2)对锁的处理机制不同。由于sleep方法的主要作用是让线程休眠指定的一段时间,在时间到时自动恢复,不涉及线程间的通信,因此,调用sleep方法并不会释

放锁。而wait方法则不同,当调用wait方法后,线程会释放掉它所占用的锁,从而使线程所在对象的其他synchronized数据可被其他线程使用。举个简单例子,在小

明拿遥控器期间,他可以用自己的sleep方法每隔十分钟掉一次电视台,而在他调台休息的十分钟期间,遥控器还在他的手上。

    3)使用区域不同。由于wait方法的特殊意义,所以,它必须放在同步控制方法或者同步语句块使用,而sleep则可以放在任何地方使用。

    4)sleep方法必须捕获异常,而wait,notify以及notifyall不需要捕捉异常。在sleep的过程中,有可能被其他对象调用它的interrupt(),产生InterruptedException

异常。由于sleep不会释放“锁标志”,容易导致死锁问题的发生,所以,一般情况下,不推荐使用sleep方法,而推荐使用wait方法。

————————

Sleep () is a method to pause a thread for a period of time. Wait () is also a method to pause the thread. For example, when the thread interacts, if the thread sends a wait () call request to a synchronous thread x,

Then the thread will pause execution and the called object will enter the waiting state until it is awakened or the waiting time expires.

Specifically, sleep differs from wait in the following aspects:

1) different principles. Sleep is a static method of thread class. It is used by a thread to control its own process. It will make this thread suspend execution for a specified time, and give the execution opportunity to other threads until the timing time expires,

This thread wakes up automatically. For example, when a thread executes the time reporting function and prints a time every second, it needs to add a sleep method in front of the print method so that it can execute it every other second,

The process is like an alarm clock. Wait is a method of the object class, which is used for inter thread communication. This method will make the process that currently owns the object lock wait until other threads call the notify method (or notifyAll method)

When I woke up. A developer can also assign it a time to wake up automatically. The methods associated with wait include notify and notifyAll.

2) different lock handling mechanisms. The main function of sleep method is to let the thread sleep for a specified period of time and automatically recover when the time expires, which does not involve the communication between threads. Therefore, calling sleep method will not release the thread

Put the lock. The wait method is different. When the wait method is called, the thread will release the lock it occupies, so that other synchronized data of the object where the thread is located can be used by other threads. For a simple example, in small

During the period of taking the remote control, he can use his sleep method to drop the TV station every ten minutes, and during the ten minute rest of his station transfer, the remote control is still in his hand.

3) different use areas. Because of the special significance of the wait method, it must be used in the synchronization control method or synchronization statement block, while sleep can be used anywhere.

4) the sleep method must catch exceptions, while wait, notify and notifyAll do not need to catch exceptions. During sleep, other objects may call its interrupt() to generate interruptedexception

Abnormal. Since sleep does not release the “lock flag”, which can easily lead to deadlock problems, the sleep method is generally not recommended, but the wait method is recommended.