Difference between Sleep and Wait in Java Multithreading

Source: Internet
Author: User

Multithreading in Java is a preemptible mechanism, not a time-sharing mechanism. The preemption mechanism is that multiple threads are running, but only one thread is running.

1. They are all in a multi-threaded environment, and can block the specified number of milliseconds in the program call and return.
2. Both wait () and sleep () can interrupt the pause state of the thread through the interrupt () method, so that the thread immediately throws InterruptedException.
If Thread A wants to terminate Thread B immediately, it can call the interrupt method for the Thread instance corresponding to Thread B. If thread B is wait/sleep/join at the moment, thread B will immediately throw InterruptedException and directly return in catch () {} to end the thread safely.
It should be noted that InterruptedException is thrown by the thread itself from the internal, not by the interrupt () method. When interrupt () is called for a thread, if the thread is executing common code, the thread will not throw InterruptedException. However, once the thread enters wait ()/sleep ()/join (), InterruptedException is immediately thrown.

1. Thread class Methods: sleep (), yield (), etc.
Object Methods: wait () and Policy ()
2. Each object has a lock to control synchronous access. The Synchronized keyword can interact with the object lock to synchronize threads.
The sleep method does not release the lock, but the wait method releases the lock, so that other threads can use synchronous control blocks or methods.
3. wait, policy, and policyall can only be used in synchronous control methods or synchronization control blocks, while sleep can be used anywhere.
4. sleep must capture exceptions, while wait, policy, and policyall do not need to capture exceptions.
Thread Scheduling

The thread scheduler selects a high-priority thread (entering the running state) for execution based on the priority of the thread, and the thread scheduling is preemptive. That is, if the thread is being executed in the current thread, if a thread with a higher priority enters the running state, the thread is immediately scheduled for execution.
Preemptive scheduling is divided into the time slice mode and the exclusive mode. In the time slice mode, after the current active thread executes the current time slice, if there are other threads with the same priority in the ready state, the system will give the execution permission to other ready threads with the same priority; the current active thread is transferred to the queue waiting for execution, waiting for scheduling of the next time slice.
In the exclusive mode, once the active thread obtains the execution right, it will continue until the execution is completed, or the CPU is automatically abandoned for some reason, or the thread with a high priority is in the ready state.

The first thing I want to talk about is: in multi-thread programming, sleep and wait operations are often used. The biggest difference between sleep and wait is that sleep wait will not release the current lock, wait is the opposite. Today I come up with a realistic example to compare the differences between the two.
Here is an example of buying tickets at the train station. Let's go to the train station to buy a ticket. When the bus stops at the ticket window, we will tell the conductor where to get the ticket. When will the bus depart, the conductor will print the ticket for us through the computer. At this time, we will have to wait. If we use sleep mode, we have to wait until the conductor gives us the ticket. If we use wait mode, we will leave our site at this time, the friends behind the ticket may be standing in my current position (in front of the window). If we want to get the ticket, we have to repeat the back of the team to get the ticket.

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.