A detailed introduction to the lifecycle of Java threads and instance code _java

Source: Internet
Author: User
Tags thread class

When the thread is created and started, it either goes into execution or is not always executing, and in its lifecycle is new, Ready (Runnable), run (Running '), blocked (Blocked), and ".", and " The five states of Death (Dead). After the thread is created, it is not possible to occupy the CPU running independently, and it needs to switch between multiple threads, so most of the time is switched between running and blocking.

The state of a thread

There are several different states of the existence of threads, as follows:

    1. New state
    2. Ready state
    3. Running state
    4. Dead state
    5. Non runnable State

1, new state

The new state is the state in which the thread has been created, but has not yet started running. This state enables the thread to run by calling the thread's start () method.

2, runnable State

The runnable state can be called a ready to run state, or a queue, which enables the thread to run by calling the thread's start () method.
The thread scheduler determines which threads to run and how long the thread runs.

3, Running state

If a thread is executing, it is in a running state.

4, Dead State

Once a thread has entered the dead state, it is no longer able to run.

5, Non runnable state

    1. A running thread can be converted to a non runnable state, depending on the operation.
    2. A thread can also remain non runnable state until the condition that satisfies it appears.
    3. A non runnable state thread cannot jump directly to the running state, but must first be converted to a runnable state.
    4. Sleep sleeping: The time specified by the thread sleep.
    5. I/O blocking: The thread waits until the blocking operation completes.
    6. Join blocking: The thread waits until another thread finishes executing.
    7. Wait for notification: The thread waits for another thread to be notified.
    8. Lock mechanism blocking: the thread waits until the specified lock is freed and the lock is acquired.

The Java Virtual machine JVM executes threads based on the thread's priority and scheduling principles.

Second, the thread scheduler

In the JVM, the implementation of the thread scheduler is typically based on the following two strategies:

Preemptive scheduling strategy
Time-sharing scheduling strategy or round-robin cyclic scheduling strategy

The implementation of the thread scheduler is not platform-independent, so the scheduling of threads is unpredictable.

Third, the priority of the thread

The JVM assigns a priority to each newly created thread.

Level 0: This is the lowest priority
Level 5: It's a normal priority.
Level 10: This is the highest priority

To save these values, the thread class has three corresponding variables:

    1. public static final int min_priority
    2. public static final int norm_priority
    3. public static final int max_priority

A thread first inherits the priority of its parent thread, the default priority of each thread is level 5 (normal priority), and the main thread's default priority is level 5.

You can set the priority of a thread through the setpriority (int priority) method.

Public final void setpriority (int priority)
public void getpriority ();

A user-defined thread whose default thread name is Thread+ ordinal, with ordinal number starting at 0, such as the first thread being Thread0.
The thread name can be set through the SetName (String name) method, and the GetName () method is used to obtain the name of the thread.

Public final void SetName (String name)
Public final String GetName ().

Instance

Let's look at an example:

Package demo.ch;
  public class Userthread extends Thread {userthread () {super ();
  } userthread (String name) {super (name);
  public void Run () {System.out.println ("thread started running ...");
    public static void Main (string[] args) {Userthread thread1 = new Userthread ("Thread1");

    Userthread thread2 = new Userthread ("Thread2");
    System.out.println ("Thread 1 Initial name and priority");
    System.out.println ("Name:" + thread1.getname ());

    System.out.println ("Priority:" + thread1.getpriority ());
    System.out.println ("Thread 2 initial name and priority");
    System.out.println ("Name:" + thread2.getname ());
    System.out.println ("Priority:" + thread2.getpriority ());

    System.out.println ("");
    Thread1.setpriority (6);

    Thread2.setpriority (9);
    System.out.println ("Thread 1 Initial name and priority");
    System.out.println ("Name:" + thread1.getname ());

    System.out.println ("Priority:" + thread1.getpriority ()); System.oUt.println ("Thread 2 initial name and priority");
    System.out.println ("Name:" + thread2.getname ());
    System.out.println ("Priority:" + thread2.getpriority ());

    System.out.println ("");
    Thread1.start ();

    Thread2.start ();
  for (int i=0; i<5; i++) System.out.println ("Main method I value:" + i);

 }
}

Output results:

Thread 1 Initial name and priority
name:thread1
priority:5
thread 2 initial name and priority
Name: Thread2
priority:5

thread 1 Initial name and priority Name:thread1 priority:6
thread 2 Initial Name and priority
NAME:THREAD2
priority:9

Main method I value:0
Main method i value:1
thread s Tarted running.
Main method I value:2
thread started running.
Main method I value:3 the
main method I Value:4

Thank you for reading, I hope to help you, thank you for your support for this site!

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.