The difference between sleep and wait in Java threads detailed introduction to _java
Source: Internet
Author: User
Multithreading in Java is a preemptive mechanism, not a time-sharing mechanism. The preemption mechanism is that multiple threads are in a running state, but only one thread is running.
Common Ground:
1. They are all in a multi-threaded environment, can block the specified number of milliseconds at the program's call, and return.
2. Wait () and sleep () can interrupt the paused state of the thread through the interrupt () method, causing the thread to throw interruptedexception immediately.
If thread A wants to end thread B now, you can call the interrupt method on thread B's corresponding threads instance. If thread B is wait/sleep/join at the moment, thread B throws the interruptedexception immediately, and the thread is safely closed by direct return in catch () {}.
It should be noted that the interruptedexception is thrown by the thread itself, not by the interrupt () method. When interrupt () is invoked on a thread, the thread does not throw interruptedexception if it is executing normal code. However, once the thread has entered the wait ()/sleep ()/join (), the interruptedexception is thrown immediately.
different points:
Methods of the 1.Thread class: Sleep (), yield (), etc.
Method of object: Wait () and notify ()
2. Each object has a lock to control synchronous access. The Synchronized keyword can interact with the lock on the object to achieve synchronization of the thread.
The Sleep method does not release the lock, and the wait method frees the lock so that other threads can use the synchronized control block or method.
3.wait,notify and Notifyall can only be used in synchronous control methods or in synchronized control blocks, while sleep can be used anywhere
4.sleep must catch exceptions, while wait,notify and notifyall do not need to catch exceptions
Scheduling of Threads The thread scheduler selects high priority threads (into a running state) by thread priority, while thread scheduling is preemptive, that is, if a higher-priority thread enters a running state during the current thread execution, the thread is immediately scheduled to execute.
Preemptive scheduling is divided into: time slice mode and exclusive way. In the time slice mode, when the current active thread finishes executing the current time slice, if there are other threads of the same priority in the ready state, the system will give execution to the same priority thread as the other ready states, and the current active thread goes into the waiting execution queue for the next time slice schedule.
In exclusive mode, the currently active thread, once executed, continues until execution is completed or the CPU is actively discarded for some reason, or a high-priority thread is in the ready state.
Give an example to explain The first thing I would like to say is that in multithreaded programming, sleep and wait operations are often used, and we understand that the biggest difference between sleep and waiting is that sleep waits do not release the current lock, but instead. Today I think of a practical example to compare the difference between the two.
I have to go to the railway station to buy tickets to illustrate the example. We go to the railway station to buy tickets, when standing to the ticket window to tell the conductor where to go tickets, when the departure, the conductor will pass the computer to print tickets to us, this time we have to wait. If we use sleep mode, we have to stand there and wait, until the conductor gave us a ticket, if we use the wait mode, this time to leave the position of our station, the back of the fancier may stand in my current position (window), we want to take the ticket, we have to rearrange the back of the queue to pick up the tickets.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service