Home > Java > javaTutorial > What is the difference between sleep() and wait()?

What is the difference between sleep() and wait()?

云罗郡主
Release: 2018-10-15 13:55:19
forward
3354 people have browsed it

This article brings you what is the difference between sleep() and wait()? , has certain reference value, friends in need can refer to it, I hope it will be helpful to you.

What is the difference between sleep() and wait().

sleep is a method of the thread class (Thread), which causes this thread to suspend execution for a specified time and give execution opportunities to other threads, but the monitoring status is still maintained and will automatically resume when the time comes. Calling sleep does not release the object lock. wait is a method of the Object class. Calling the wait method on this object causes this thread to give up the object lock and enter the waiting lock pool waiting for this object. Only after the notify method or notifyAll is issued for this object, this thread enters the object lock pool and prepares to obtain it. The object lock enters the running state.

When a thread enters a synchronized method of an object, whether other threads can enter other methods of this object.

Other threads can only access other non-synchronized methods of the object, but synchronized methods cannot enter.

What are the thread synchronization methods?

wait(): puts a thread in a waiting state and releases the lock of the object it holds;

sleep(): puts a running thread in a sleep state, which is a static Method, call this method to catch InterruptedException exception;

notify(): wake up a thread in waiting state. Note that when calling this method, you cannot exactly wake up a thread in waiting state. Instead, the JVM determines which thread to wake up, and not by priority;

notifyAll(): wakes up all threads in the waiting state. Note that it is not to give all awakened threads an object lock, but to let them compete. .

There are several implementation methods for multi-threading and several implementation methods for synchronization.

There are two implementation methods for multi-threading, namely inheriting the Thread class and implementing the Runnable interface;

There are two implementation methods for synchronization, namely synchronized, wait and notify.

What are the similarities and differences between synchronization and asynchronousness, and under what circumstances are they used?

If the data will be shared among threads. For example, the data being written may be read by another thread in the future, or the data that has been read has been written by another data, then these data are shared data and must be accessed synchronously. Asynchronous programming should be used when the application is calling a method that takes a long time to execute and does not want the program to wait for the return of the method. In many cases, the asynchronous approach is often more efficient.

Use run() or start() to start a thread.

Starting a thread is to call the start() method to make the virtual machine represented by the thread in a runnable state, which means that it can be scheduled and executed by the JVM. This does not mean that the thread will run immediately. The run() method can stop a thread by generating a must-exit flag.

The basic concept of threads, the basic status of threads, and the relationship between states.

Thread refers to an execution unit that can execute program code during the execution process. Each program has at least one thread, which is the program itself;

There are four types of threads in Java The statuses are: running, ready, suspended, and finished.

Briefly describe the similarities and differences between synchronized and java.util.concurrent.locks.Lock.

Main similarities: Lock can complete all functions implemented by synchronized;

Main differences: Lock has more precise thread semantics and better performance than synchronized. synchronized will automatically release the lock, but Lock must require the programmer to release it manually, and it must be released in the finally clause.

How many methods can be used to implement a thread in java? What keywords are used to modify synchronization methods? Why are stop() and suspend() deprecated?

There are two implementation methods, namely inheriting Thread and implementing the Runnable interface;

Use synchronized to modify the synchronization method;

Object to using stop() because it is unsafe. It releases all locks acquired by threads, and if the objects are in an incoherent state, other threads can inspect and modify them in this state. As a result it is difficult to detect the real problem.

The suspend() method is prone to deadlock. When suspend() is called, the target thread will stop, but it still holds the lock acquired before. At this point, no other thread can access the locked resource unless the "suspended" thread resumes operation. For any thread, if they want to resume the target thread and try to use any locked resource at the same time, a deadlock will occur. Therefore, you should not use suspend(), but put a flag in your Thread class to indicate whether the thread should be active or suspended. If the flag indicates that the thread should be suspended, use wait() to order it to enter the waiting state. If the flag indicates that the thread should be resumed, use a notify() to restart the thread.

The above is a complete introduction to the differences between sleep() and wait(). If you want to know more aboutJava video tutorial, please pay attention to PHP Chinese website.

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

Related labels:
source:cnblogs.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template