The difference between sleep and wait in Java Multi-threading

Source: Internet
Author: User
Tags ticket

Multithreading in Ava is a preemptive mechanism, not a time-sharing mechanism. The preemptive mechanism is that multiple threads are in a running state, but only one thread is running.

Common:

1. They are all in multi-threaded environment, can block the number of milliseconds specified at the call of the program, and return.

2. Wait () and sleep () can break the thread's suspend state through the interrupt () method, causing the thread to throw interruptedexception immediately.

If thread A wants to end thread B immediately, you can call the interrupt method on the thread instance that corresponds to threads B. If thread B is wait/sleep/join at the moment, thread B will immediately throw interruptedexception, and return directly in catch () {} to safely end the thread.

It is important to note that Interruptedexception is thrown from within the thread itself, not by the interrupt () method. When you call interrupt () on a thread, the thread does not throw interruptedexception at all if it is executing normal code. However, once the thread has entered wait ()/sleep ()/join (), the interruptedexception is immediately thrown.

Different points:

1. Method of the Thread class: sleep (), yield (), etc.

Method of object: Wait () and notify (), etc.

2. Each object has a lock to control synchronous access. The Synchronized keyword can interact with the object's lock to achieve thread synchronization.

The Sleep method does not release the lock, and the wait method frees the lock so that other threads can use the synchronization control block or method.

3. Wait,notify and Notifyall can only be used in a synchronous control method or in a sync control block, and 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 chooses a high-priority thread (into the running state) to execute at the priority of the thread, and the thread schedule is a preemptive dispatch, that is, if a higher-priority thread enters the operational state during the execution of the current thread, the thread is scheduled to execute immediately.

Preemptive scheduling is divided into: time slice mode and exclusive mode. 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 hand over the execution to the same priority thread of the other ready states, and the current active thread goes to the waiting execution queue, waiting for the next time slice to be dispatched.

In exclusive mode, once the current active thread has been executed, it will be executed until execution is complete or the CPU is actively discarded for some reason, or a thread with a high priority is in the ready state.

Examples Show

The first thing I want to say is that in multithreaded programming, sleep and wait operations are often used, and we understand that the biggest difference between sleep and wait is that sleep waits does not release the current lock, while wait is the reverse. Today I think of a realistic example to compare the difference between the two.
I take the example of buying a ticket to the railway station to illustrate. We go to the railway station to buy tickets, when we stand to the ticket window to tell the conductor where the ticket needs to go, when the departure, the conductor on the computer to print the ticket to us, this time we have to wait. If we use sleep mode, we have to stand there and wait until the conductor gives us the ticket, and if we use the wait mode, we will leave our position at this time, and the fancier may stand at my current position (front of the window), and we will have to pick up the ticket in the back of the queue.

The difference between sleep and wait in Java Multi-threading

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.