search
HomeJavaJavaBaseWhat is the difference between wait and sleep in java?

What is the difference between wait and sleep in java?

sleep:

The sleep method belongs to the method in the Thread class, which means to put a thread into sleep state and wait for a certain period of time before automatically waking up. to enter the runnable state, it will not enter the running state immediately, because the thread scheduling mechanism also takes time to resume the running of the thread. After a thread object calls the sleep method, it will not release all the object locks it holds, so it It will not affect the operation of other process objects. However, during the sleep process, its interrupt() may be called by other objects, resulting in an InterruptedException exception. If your program does not catch this exception, the thread will terminate abnormally and enter the TERMINATED state. If your program catches this exception, Exception, then the program will continue to execute the catch statement block (and possibly the finally statement block) and subsequent code.

#wait:

wait is a member method belonging to Object. Once an object calls the wait method, the notify() and notifyAll() methods must be used to wake up the process; If a thread owns a synchronization lock on one or more objects, then after calling wait(), the thread will release all synchronization resources it holds, not limited to the object on which the wait() method was called. The wait() method may also be generated by other objects calling the interrupt() method during the wait process.

The difference between sleep and wait methods is:

● Sleep comes from the Thread class, and wait comes from the Object class

● The sleep method does not release the lock. The wait method releases the lock so that other threads can use the synchronization control block or method

●wait, notify and notifyAll can only be used in the synchronization control method or synchronization control block, while sleep can be used anywhere

● Sleep must catch exceptions, while wait, notify and notifyAll do not need to catch exceptions

Recommended learning: Java video tutorial

The above is the detailed content of What is the difference between wait and sleep in java?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool