Home > Java > javaTutorial > body text

Java thread status and common methods analysis and examples

王林
Release: 2023-04-21 13:04:08
forward
1459 people have browsed it

You can obtain the status of the thread through the Thread.getState method (there are 6 states of threads in total)

NEW (new) new: not started yet

RUNNABLE (runnable state) runnable: in progress Running in JVM; or waiting for other resources of the operating system (such as processor)

//Some programming languages ​​will divide RUNNABLE into two situations
//1.running
/ /2.ready
//The above two types are all RUNNABLE in Java

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

WAITING (Waiting state) waiting: Waiting for another thread

Calling the following method will be in the waiting state

Object.wait without timeout value

Thread.join without timeout value

LockSupport.park

//Will be introduced in detail later

TIMED_WAITING (timed waiting state) timed_waiting

Calling the following method will be in the timed waiting state

Thread.sleep

Object.wait with timeout value

Thread.join with timeout value

LockSupport.parkNanos

LockSupport. parkUnti

//Will be introduced in detail later

TERMINATED (termination status): Execution completed

Some examples:

public class Main {
	public static void main(String[] a) {
		Thread dangqian = Thread.currentThread();//获取主线程
		System.out.println(dangqian.getState());//输出RUNNABLE
		//表示主线程正处于RUNNABLE状态
		
		Thread tj = new Thread();//新建1个线程
		System.out.println(tj.getState());//输出NEW
		
		Thread tj1 = new Thread(new Runnable() {
			@Override
			public void run() {
				System.out.println("ACM");
			}
		});
		tj1.start();
		//启动执行完run方法后线程终止 此时处于TERMINATED状态
	}
}
Copy after login

sleep interrupt introduction

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

During the pause period, if the interrupt method of the thread object is called to interrupt the thread, a java.lang.InterruptedException exception will be thrown

The example (comment) introduction is more clear:

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是静态方法,所以通过类名来调用;让线程睡3s
				}catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("2");
			}
		});
		tj.start();
		//因为是多线程,在执行tj线程时,主线程依然往下走
		try {
			Thread.sleep(1000);//让主线程睡1s
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("3");
		//由于tj线程睡得较久,所以先打印3,再打印2
		tj.interrupt();//中断此线程,但实际上只是给线程设置一个中断标志,线程仍会执行;在这里用于触发异常
	}
}
Copy after login

Output:

1
3
2

join isAlive Introduction

A.join method: After thread A completes execution, the current thread continues to execute the task. You can pass parameters to specify the maximum waiting time

A.isAlive method: check whether thread A is still alive

Instance:

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是静态方法,所以通过类名来调用;让线程睡3s
				}catch (InterruptedException e) {
					e.printStackTrace();
				}
				System.out.println("2");
			}
		});
		tj.start();
		System.out.println(tj.isAlive());//此时3s未过,tj线程仍在执行;所以输出true
		try {
			tj.join();//等tj线程执行完毕再往下执行
			//tj.join(1000); 等待tj线程1s再往下执行
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("3");
	    System.out.println(tj.getState());
	    System.out.println(tj.isAlive());//tj线程已执行完毕;所以输出false
	}
}
Copy after login

Output:

true
1
2
3
TERMINATED
false

The above is the detailed content of Java thread status and common methods analysis and examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template