Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The State of Java Thread and case Analysis of Common methods

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the Java thread state and commonly used method case analysis related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will gain something after reading this Java thread state and common method example analysis article, let's take a look at it.

The state of a thread can be obtained through the Thread.getState method (threads have a total of 6 states)

NEW (New) new: not started yet

RUNNABLE (runnable status) runnable: running in JVM, or waiting for other resources of the operating system (such as processors)

/ / some programming languages divide RUNNABLE into two situations

/ / 1.running

/ / 2.ready

/ / both of the above belong to RUNNABLE in Java

BLOCKED (blocking state) blocked: waiting for monitor lock (internal lock)

WAITING (wait status) waiting: waiting for another thread

Calling the following method will be in a waiting state

Object.wait with no timeout value

Thread.join with no timeout value

LockSupport.park

/ / it will be described in detail later.

TIMED_WAITING (timed wait status) timed_waiting

If you call the following method, you will be waiting at a fixed time.

Thread.sleep

Object.wait with timeout value

Thread.join with timeout value

LockSupport.parkNanos

LockSupport.parkUnti

/ / it will be described in detail later.

TERMINATED (termination status): execution has been completed

Some examples:

Public class Main {public static void main (String [] a) {Thread dangqian = Thread.currentThread (); / / get the main thread System.out.println (dangqian.getState ()); / / output RUNNABLE / / indicates that the main thread is in the RUNNABLE state Thread tj = new Thread () / / create a new thread System.out.println (tj.getState ()); / / output NEW Thread tj1 = new Thread (new Runnable () {@ Override public void run () {System.out.println ("ACM")) }}); tj1.start (); / / Thread terminates after starting the execution of the run method and is in the TERMINATED state}}

Sleep interrupt introduction

You can pause the current thread and enter the WAITING state through the Thread.sleep method

During the pause, if the thread is interrupted by calling the interrupt method of the thread object, a java.lang.InterruptedException exception is thrown

The introduction of examples (notes) is clearer:

Public class Main {public static void main (String [] a) {Thread tj = new Thread (new Runnable () {@ Override public void run () {System.out.println ("1") Try {Thread.sleep (3000); / / sleep is a static method, so it is called through the class name Let the thread sleep for 3 s} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("2");}}) Tj.start (); / / because it is multithreaded, when executing the tj thread, the main thread still goes down try {Thread.sleep (1000); / / Let the main thread sleep 1s} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("3"); / / because the TJ thread sleeps for a long time, print 3 first, and then print 2 tj.interrupt (); / / interrupt the thread, but actually just set an interrupt flag for the thread, and the thread will still execute Used here to trigger an exception}}

Output:

one

three

two

Join isAlive introduction

A.join method: wait for thread A to finish execution, and then the current thread continues to execute the task. You can pass parameters to specify the maximum waiting time

A.isAlive method: check to see if thread An is still alive

Example:

Public class Main {public static void main (String [] a) {Thread tj = new Thread (new Runnable () {@ Override public void run () {System.out.println ("1") Try {Thread.sleep (3000); / / sleep is a static method, so it is called through the class name Let the thread sleep for 3 s} catch (InterruptedException e) {e.printStackTrace ();} System.out.println ("2");}}) Tj.start (); System.out.println (tj.isAlive ()); / / 3s have not passed and the tj thread is still executing So output true try {tj.join (); / / wait for the TJ thread to finish execution before / / tj.join (1000); wait for the TJ thread to execute for 1s and then execute} catch (InterruptedException e) {e.printStackTrace () } System.out.println ("3"); System.out.println (tj.getState ()); System.out.println (tj.isAlive ()); / / the tj thread has finished executing So output false}}

Output:

True

one

two

three

TERMINATED

False

This is the end of the article on "Java thread state and example analysis of common methods". Thank you for reading! I believe that you all have a certain understanding of the knowledge of "Java thread state and common method instance analysis". If you want to learn more knowledge, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report