Multithreading - Concurrent Programming (4) - Introduction to the Status of Threads and Some Common Methods

The state of the thread can be obtained through the Thread.getState method (there are 6 states of the thread)

NEW (new) new: not yet started

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

//有些编程语言会把RUNNABLE分成2种情况
//1.running
//2.ready
//以上2种在Java中都属于RUNNABLE

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

WAITING (waiting state) waiting: waiting for another thread ,
calling the following methods will be in a waiting state
Object.wait
without a timeout value Thread.join
LockSupport.park without a timeout value
//will be introduced in detail later

TIMED_WAITING (timed waiting state) timed_waiting calls the following
methods to be in a timed waiting state.
Thread.sleep
has a timeout value. Object.wait
has a timeout value
.

TERMINATED (termination state): some instances have been executed :

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状态
	}
}

sleep interrupt introduction

You can suspend the current thread through the Thread.sleep method and enter the WAITING state.
During the suspension 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 (note) introduction is more explicit: 

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();//中断此线程,但实际上只是给线程设置一个中断标志,线程仍会执行;在这里用于触发异常
	}
}
输出:
1
3
2

join isAlive Introduce
the A.join method: after thread A is finished executing, the current thread will continue to execute the task. You can pass parameters to specify the longest waiting time
A.isAlive method: check whether thread A 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是静态方法,所以通过类名来调用;让线程睡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
	}
}
输出:
true
1
2
3
TERMINATED
false

Guess you like

Origin blog.csdn.net/weixin_59624686/article/details/124041842