The life cycle of a thread (v)

Source: Internet
Author: User
Tags thread class

This blog describes the life cycle of threads.

A thread is a process that executes dynamically, and it also has a process from creation to death.

In the Thread class, there is an enumeration inner class:

  

The above information is represented by a picture as follows:

First Picture:

  

The second picture: waiting, waiting, blocking as blocking a state

  

1. New State (new): Create a thread with new, just allocate memory space in the heap

In the new state, the thread has not yet called the start () method, but there is only one thread object.

Thread t = new thread ();//This is the new state of the T thread

2. Operational status (runnable): New state calls the start () method to enter the operational state. And this is divided into two states, ready and running, respectively, indicating the readiness state and the running state.

Ready state: The thread object calls the start () method, waits for the JVM to Dispatch, (at which point the thread is not running)

Run state: Thread object gets JVM scheduler, runs multiple threads running in parallel if multiple CPUs are present

Note: Thread objects can only be called once by the Start () method, otherwise error: Illegathreadstateexecptiong

3, blocking state (blocked): The running thread for some reason to abandon the CPU, temporarily stop running, will enter the blocking state. At this point the JVM does not allocate the CPU to the thread and knows that the thread is back in a ready state before the opportunity goes to the running state.

Note: Blocking status can only be entered in the ready state, not directly into the running state

The blocking state is divided into two situations:

①, when thread A is in the operational state, attempting to acquire a synchronization lock, is obtained by the B thread, at which point the JVM puts the current a thread into the lock pool and a thread goes into a blocking state

②, when the thread is in the running state, an IO request is made and the blocking state is entered

4. Wait state (Waiting): Wait state can only be awakened by other threads, using the no parameter Wait () method

①, when the thread is in the running state, calls the Wait () method, at which point the JVM puts the thread into the waiting pool

5. Timed wait (timed waiting): Call the Wait (long time) or sleep (long time) method with parameters

①, when the thread is in the running state, calls the WITH parameter wait method, at which time the JVM puts the thread into the waiting pool

②, the current thread called the Sleep (long time) method

6. Termination status (terminated): Commonly known as the Death state, indicates thread termination

①, normal termination, execute run () method, end normally

②, forcing termination, such as calling the Stop () method or the Destory () method

③, abnormal termination, exception occurred during execution

Here are a few ways to learn more about threading:

  1. Sleep (long Millis) thread hibernation: Let the executing thread pause for a period of time and enter the timed wait state.

static void sleep (Long Millis): After this method is called, the current thread discards the CPU resources, and within a specified time, the thread in which sleep is not given a chance to run, the thread in this state does not release the sync Lock (note and wait () differences, wait Will discard the CPU resources and also discard the sync lock)

This method is more used to simulate the network delay, so that multi-threaded concurrent access to the same resource when the error effect more obvious.

   2. Join () Union thread: Indicates that this thread waits for another thread to finish (death) before execution, and the thread object is blocked after the join method is called. Which thread is written on, which thread is blocking

    This is also called a union thread, which means that the current thread and the thread on which the current thread is located are combined into one thread

    

?
123456789Ten One A - - the - - - + - + A at - - - - - in - to package com.ys.thread; class Join extends Thread{    @Override    public void run() {        for(int i = 0 ; i < 10 ;i++){            System.out.println("播放音乐"+i);        }    }} public class ThreadTest {    public static void main(String[] args) {        //创建 join 线程对象        Join joinThread = new Join();        for(int i = 0 ; i < 10 ; i++){            System.out.println("玩游戏"+i);            if(i==3){                joinThread.start();            }            if(i==5){                try {                    joinThread.join();//强制运行 join 线程,知道 join 运行完毕了,main 才有机会运行                } catch (InterruptedException e) {                    e.printStackTrace();                }            }        }    } }

Results:

?
1 2 3 4 5 6 7 8 9 Ten One A - - the - - - + - 玩游戏0玩游戏1玩游戏2玩游戏3玩游戏4玩游戏5播放音乐0播放音乐1播放音乐2播放音乐3播放音乐4播放音乐5播放音乐6播放音乐7播放音乐8播放音乐9玩游戏6玩游戏7玩游戏8玩游戏9

  

Background thread (daemon): A thread running in the background that is intended to serve other threads, also known as a daemon thread.

①, the garbage collection thread of the JVM is the daemon thread.

②, Main method is foreground thread, not background thread

  

?
1 2 3 4 5 6 7 public static void main(String[] args) {        String mainThreadName = Thread.currentThread().getName();        System.out.println(mainThreadName);  //main                 System.out.println(Thread.currentThread().isDaemon());//false             }

The life cycle of a thread (v)

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.