Java thread (thread)--wait, sleep, notify, thread lifecycle, state

Source: Internet
Author: User
Tags thread class

Or analyze the interrupt of the thread from the code based on the instance:

Package lyfpractice;

/**
 * Created by Fangjiejie on 2016/12/15.
 *
/public class Sthread {

}
class S extends thread{
    @Override public
    void Run () {for
        int i=0;i& lt;1000;i++) {
            if (i==100) {
//                1.thread.currentthread (). Stop (); not recommended
//                try {
//                    Thread.CurrentThread (). Sleep (Long.max_value); it is also more memory-intensive
///                                              (1000) The time inside can also be controlled by oneself
/                } catch (Interruptedexception e) {
//                    e.printstacktrace ();
                BREAK;//3. Recommended to use
            }
            System.out.println (Thread.CurrentThread (). GetName () + ":" +i);
        }
}



Usage:
Sleep ():
Lets the currently executing thread hibernate (suspend execution) within the specified number of milliseconds, which is affected by the system timer and scheduler precision and accuracy.
Because the sleep () method is a method of the thread class, it cannot alter the object's machine lock. So when you call sleep () in a synchronized method, the thread is dormant, but the object's machine lock is not freed, and the object is still inaccessible to other threads. The sleep () method does not need to be executed in a synchronized block of code. But sleep () can interrupt the paused state of the thread through the interrupt () method, causing the thread to throw interruptedexception immediately.

Wait ():
The wait () method releases the drop lock while the thread sleeps, and other threads can access the object. Wait () must be executed in a synchronized block of code. When a thread executes to the wait () method, it enters a waiting pool associated with the object, loses the machine lock of the object, and allows other threads to perform some synchronization operations. However, wait () can interrupt the paused state of the thread through the interrupt () method, causing the thread to throw interruptedexception immediately.

Notify ():
Notify () wakes up a single thread waiting on this object monitor. When it is awakened by a notify () method, the thread in the wait pool is placed in the lock pool. The thread waits to get the machine lock from the lock pool and then back to the interrupt scene before Wait ().
Notifyall () wakes up all threads waiting on this object monitor.

The difference between wait () and sleep ():
Call the Wait () method, and the thread discards the object lock and enters the waiting lock pool for the object. Only after the Notify () method is invoked on this object does this thread enter the object lock pool ready to get the object lock into the running state. However, if the Notify () method is not invoked, the thread is always in a pending state.
The sleep () method causes the program to suspend execution of the specified time, giving up the CPU to the other thread, but his monitoring status is still maintained and will automatically resume running when the specified time is up. During the call to the sleep () method, the thread does not release the object lock.

Thread life cycle:

Thread Status:
Thanks for turning from http://zy19982004.iteye.com/blog/1626916




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.