Similarities and differences between sleep() method and wait() method in Java

foreword

After talking about the series of methods in the previous articles wait(), let's compare it and see sleep()what are the similarities and differences between it and the method. It doesn't matter if you don't know wait()the method, I will attach the articles involved in the article. Let's start with sleep()the method and thoroughly understand the difference between the two.

1. What is the sleep() method used for?

sleep()The method can make the thread enter the WAITINGstate, and it will not occupy CPU resources, nor release the lock, and execute the subsequent code until the specified time has passed. If it is interrupted during the sleep period, an exception will be thrown, and the interrupt status flag will be cleared.

The above sentence contains a lot of information, let me read it carefully for you

2. Characteristics of the sleep() method

1. sleep()The method can make the thread enter the WAITINGstate.

This wait()is consistent with the method, which will make the thread enter the waiting state.

2. Does not occupy CPU resources.

It will not waste system resources and can be used with confidence.

3. The lock is not released.

The formula I remembered to share: the sleep() method is 抱着锁睡觉.

While the thread is sleeping, the lock will not be released, and the thread will remain in the waiting state.

4. Respond to interrupts.

When an interrupt request is encountered, the current thread is supported to be interrupted and an exception is thrown sleep interrupted.

Code and analysis refer to "Scenario 2" or "Scenario 3" in the article
"How to Stop Threads Correctly in Java (Three Scenarios)" , the code running results and analysis are very concise and comprehensive, it is recommended to read.

Three. Two code writing methods of sleep() method

1.Thread.sleep(timeout)

This method parameter can only be milliseconds, if the parameter is a negative value, an exception will be thrown. Although common, it is not recommended.

2.TimeUnit.SECONDS.sleep(timeout)

This method can receive negative parameters. When the parameter is negative, reading the source code will find that it will skip execution, so no exception will be thrown. This method is highly readable and can specify parameters such as hours, minutes, seconds, milliseconds, microseconds, etc., so it is more excellent. It is recommended to use this writing method.

Four. The similarities and differences between the sleep() method and the wait() method

1. The same

①. can block the thread

②. Can respond to interrupt

2. Differences

①. wait(), notify()The method must be written in the synchronization method to prevent deadlock and permanent waiting and make the thread safer, and the sleep()method does not need to have this restriction.

②. wait()The lock will be released after the sleep()method is called. The lock will not be released after the method is called.

③. The sleep()method must specify the time parameter; the wait()method can specify the time parameter.

④. The two methods belong to different classes. The sleep()method belongs to the Threadclass. It wait()belongs to the Objectclass and is placed in the Objectclass because Javaeach class can be a lock.

Summarize

This article explains the sleep()main knowledge of the method and compares the difference between the wait()method and the sleep()method. If you like this article, please subscribe, like, and follow. For other articles on multithreading, please click the link below.

Suggested collection:

For a series of tutorials about synchronized关键字, wait(), and notify()methods, please refer to the following articles:

"Thread-safe and thread-unsafe analysis and examples in Java"

"Two Ways of Creating Threads in Java Official Documents and Analysis of Advantages and Disadvantages"

"How to stop threads correctly in Java (three scenarios)"

Producer-Consumer Patterns of Design Patterns

"Java multi-threaded wait() and notify() series method usage tutorial"

"The use of notifyAll() method in Java multithreading tutorial"

"Java two threads alternately print odd and even numbers (two methods comparison)"

"Two Ways and Principles of Synchronized Implementing Class Locks in Java"

"Two ways and principle analysis of synchronized implementation of object lock in Java"

"Analysis and Code Verification of Synchronized Reentrancy and Uninterruptibility in Java"

"Eight usage scenarios of Java multi-threaded access to Synchronized synchronization methods"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324172151&siteId=291194637