What are the 6 states and life cycles of Java threads?
1. Thread state (life cycle)
A thread can only be in one state at a given point in time.
Threads can have the following 6 states:
New (newly created): unstarted thread;
Runnable (Runnable): A runnable thread that needs to wait for operating system resources;
Blocked (Blocked): A thread that is blocked while waiting for the monitor lock;
Waiting (waiting): waiting for the wake-up state, waiting indefinitely for another thread to wake up;
Timed waiting (timed waiting): waiting within the specified waiting time A thread that another thread performs an operation on;
Terminated: A thread that has exited.
To determine the current state of a thread, you can call the getState method
Thread state relationship diagram
Note: dotted box (all uppercase English) status is the Java thread status.
2. Operation thread status
2.1. New creation status (NEW)
means that after the instantiation of the thread is completed, the thread is not started status.
Threads can be created in three ways
Override the Thread class run() method
Implement the Runnable interface
Implementing the Callable interface
A simple example summarizes three methods
public class Demo { public static void main(String[] args) throws ExecutionException, InterruptedException { /** * 1.直接重写run() 或继承Thread类再重写run() */ Thread thread = new Thread() { @Override public void run() { System.out.println("Thread"); } }; // 开启线程 thread.start(); /** * 2.lambda、内部类或线程类方式实现Runnable接口,实现run()方法 * 再交给Thread 类 */ Thread runThread = new Thread(() -> { System.out.println("Runnable"); }); // 开启线程 runThread.start(); /** * 3.lambda、内部类或线程类方式实现Callable接口,实现call()方法 * 再交给Thread 类:FutureTask本质也是Runnable实现类 */ FutureTask<String> futureTask = new FutureTask<String>(() -> { System.out.println("Callable"); return "CallableThread"; }); Thread callThread = new Thread(futureTask); // 开启线程 callThread.start(); // 获取call()方法的返回值 String s = futureTask.get(); System.out.println("call()方法的返回值:"+s); } }
Do not rewrite run() or call() The method directly instantiates the thread created by the Thread class has no practical significance;
Only threads created in the Callable method can obtain the return value of the thread.
2.2. Runnable state (RUNNABLE)
This state refers to the state entered after the thread instantiates the object and calls the start() method. The thread is in a runnable state, and if there are resources such as a processor, the program can be executed.
This state contains two steps at the operating system level: thread ready and thread running, but in the Java thread state, these two steps are collectively called Runnable (runnable) state.
The thread changes from the ready state to the running state. The key point is to see whether your thread has grabbed the CPU resource (CPU time slice). Whoever grabs it will run it, and if it doesn't grab it, wait. Because the CPU time slice (execution time) is very short, about ten milliseconds, the time for thread switching is very short, and the time for the ready state to change to the running state is also very short. This state is almost invisible during development. Changes, so in Java, the two are regarded as a whole, focusing on whether the thread can run and distinguishing it from other states, further simplifying the development of threads. If your program needs to run for a long time (such as writing an infinite loop) and the execution is not completed within a CPU time slice, then your thread will have to grab the next CPU time slice. Only after it has grabbed it can it continue to execute the program. If it has not grabbed it, it can continue to execute the program. Then you need to continue grabbing until the program execution in the thread is completed.
In fact, you should have seen this scenario before. For example, when multiple threads execute the same program and print logs to the same file, the logs of different threads will be mixed together, which is not conducive to troubleshooting. question. Common methods to solve this problem are: first, print the log to different files in different threads; second, save the log information into a string object, and print the log information to the file all at once at the end of the program. The second method is to use a time slice of the CPU to complete the printing of log information.
Note: The program can only call the start() method on threads in the newly created state. Do not call the start() method on threads in the non-newly created state. This will cause an IllegalThreadStateException exception.
2.3. Blocked state (BLOCKED)
The thread is in a waiting for monitor lock and is blocked. One thread acquired the lock but did not release it. Other threads also came to acquire the lock, but found that they could not acquire the lock and entered the blocked state.
The blocked state only exists under concurrent access by multiple threads, which is different from the latter two types of blocking caused by the thread itself entering "waiting".
Enter state
Enter synchronized code block/method
No lock obtained
Exit status
Obtained monitor lock
2.4. Wait Wake-up state (WAITING)
The whole process is like this: the thread first acquires the object lock in the synchronization method of an object; when the wait method is executed, the thread will release the object lock, and the thread is put Enter the waiting queue of this object; wait for another thread to acquire the lock of the same object, and then wake up the thread in the object's waiting queue through the notify() or notifyAll() method.
It can be known from the entire process that
wait (), notify () and notifyAll () methods need to obtain the lock before the thread can continue execution, so these three methods are required Place it in a synchronized code block/method for execution, otherwise an exception will be reported: java.lang.IllegalMonitorStateException.
在同步代码块中,线程进入WAITING 状态时,锁会被释放,不会导致该线程阻塞。反过来想下,如果锁没释放,那其他线程就没办法获取锁,也就没办法唤醒它。
进入状态
object.wait()
thread.join()
LockSupport.park()
退出状态
object.notify()
object.notifyall()
LockSupport.unpark()
2.5.计时等待状态(TIMED_WAITING)
一般是计时结束就会自动唤醒线程继续执行后面的程序,对于Object.wait(long) 方法还可以主动通知唤醒。
注意:Thread类下的sleep() 方法可以放在任意地方执行;而wait(long) 方法和wait() 方法一样,需要放在同步代码块/方法中执行,否则报异常:java.lang.IllegalMonitorStateException。
进入状态
Thread.sleep(long)
Object.wait(long)
Thread.join(long)
LockSupport.parkNanos(long)
LockSupport.parkNanos(Object blocker, long nanos)
LockSupport.parkUntil(long)
LockSupport.parkUntil(Object blocker, long deadline)
注:blocker 参数为负责此线程驻留的同步对象。
退出状态
计时结束
LockSupport.unpark(Thread)
object.notify()
object.notifyall()
2.6.终止(TERMINATED)
线程执行结束
run()/call() 执行完成
stop()线程
错误或异常>>意外死亡
stop() 方法已弃用。
3.查看线程的6种状态
通过一个简单的例子来查看线程出现的6种状态。
案例
public class Demo3 { private static Object object ="obj"; public static void main(String[] args) throws InterruptedException { Thread thread0 = new Thread(() -> { try { // 被阻塞状态(BLOCKED) synchronized (object){ System.out.println("thread0 进入:等待唤醒状态(WAITING)"); object.wait(); System.out.println("thread0 被解除完成:等待唤醒状态(WAITING)"); } System.out.println("thread0 "+Thread.currentThread().getState()); } catch (InterruptedException e) { e.printStackTrace(); } }); // 新创建状态(NEW) System.out.println(thread0.getName()+":"+thread0.getState()); Thread thread1 = new Thread(() -> { try { System.out.println("thread1 进入:计时等待状态(TIMED_WAITING)"); Thread.sleep(2); System.out.println("thread1 出来:计时等待状态(TIMED_WAITING)"); } catch (InterruptedException e) { e.printStackTrace(); } // 被阻塞状态(BLOCKED) synchronized (object){ System.out.println("thread1 解除:等待唤醒状态(WAITING)"); object.notify(); System.out.println("thread1 解除完成:等待唤醒状态(WAITING)"); } System.out.println("thread1 "+Thread.currentThread().getState()); }); // 新创建状态(NEW) System.out.println(thread1.getName()+":"+thread1.getState()); printState(thread0); printState(thread1); // 可运行状态(RUNNABLE) thread0.start(); // 可运行状态(RUNNABLE) thread1.start(); } // 使用独立线程来打印线程状态 private static void printState(Thread thread) { new Thread(()->{ while (true){ System.out.println(thread.getName()+":"+thread.getState()); if (thread.getState().equals(Thread.State.TERMINATED)){ System.out.println(thread.getName()+":"+thread.getState()); break; } } }).start(); } }
执行结果:简化后的输出结果
Thread-0:NEW
Thread-1:NEW
Thread-0:RUNNABLE
Thread-1:RUNNABLE
thread0 进入:等待唤醒状态(WAITING)
Thread-1:BLOCKED
thread1 进入:计时等待状态(TIMED_WAITING)
Thread-0:BLOCKED
Thread-0:WAITING
……
Thread-0:WAITING
Thread-1:BLOCKED
Thread-1:TIMED_WAITING
……
Thread-1:TIMED_WAITING
Thread-1:BLOCKED
……
Thread-1:BLOCKED
Thread-0:WAITING
……
Thread-0:WAITING
thread1 出来:计时等待状态(TIMED_WAITING)
Thread-0:WAITING
Thread-1:BLOCKED
thread1 解除:等待唤醒状态(WAITING)
Thread-1:BLOCKED
Thread-0:WAITING
Thread-0:BLOCKED
thread1 解除完成:等待唤醒状态(WAITING)
Thread-1:BLOCKED
thread1 RUNNABLE
Thread-0:BLOCKED
Thread-1:TERMINATED
thread0 被解除完成:等待唤醒状态(WAITING)
Thread-0:BLOCKED
thread0 RUNNABLE
Thread-0:TERMINATED
最终的执行结果如图。
注意:因为案例中使用了独立线程来打印不同线程的状态,会出现状态打印稍微延迟的情况。
The above is the detailed content of What are the 6 states and life cycles of Java threads?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Pre-formanceTartuptimeMoryusage, Quarkusandmicronautleadduetocompile-Timeprocessingandgraalvsupport, Withquarkusoftenperforminglightbetterine ServerLess scenarios.2.Thyvelopecosyste,

First,checkiftheFnkeysettingisinterferingbytryingboththevolumekeyaloneandFn volumekey,thentoggleFnLockwithFn Escifavailable.2.EnterBIOS/UEFIduringbootandenablefunctionkeysordisableHotkeyModetoensurevolumekeysarerecognized.3.Updateorreinstallaudiodriv

Using String.join() (Java8) is the easiest recommended method for connecting string arrays, just specify the separator directly; 2. For old versions of Java or when more control is needed, you can use StringBuilder to manually traverse and splice; 3. StringJoiner is suitable for scenarios that require more flexible formats such as prefixes and suffixes; 4. Using Arrays.stream() combined with Collectors.joining() is suitable for filtering or converting the array before joining; To sum up, if Java8 and above is used, the String.join() method should be preferred in most cases, which is concise and easy to read, but for complex logic, it is recommended.

Python's logging module can write logs to files through FileHandler. First, call the basicConfig configuration file processor and format, such as setting the level to INFO, using FileHandler to write app.log; secondly, add StreamHandler to achieve output to the console at the same time; Advanced scenarios can use TimedRotatingFileHandler to divide logs by time, for example, setting when='midnight' to generate new files every day and keep 7 days of backup, and make sure that the log directory exists; it is recommended to use getLogger(__name__) to create named loggers, and produce

Using PandasStyling in JupyterNotebook can achieve the beautiful display of DataFrame. 1. Use highlight_max and highlight_min to highlight the maximum value (green) and minimum value (red) of each column; 2. Add gradient background color (such as Blues or Reds) to the numeric column through background_gradient to visually display the data size; 3. Custom function color_score combined with applymap to set text colors for different fractional intervals (≥90 green, 80~89 orange, 60~79 red,

Use the .equals() method to compare string content, because == only compare object references rather than content; 1. Use .equals() to compare string values equally; 2. Use .equalsIgnoreCase() to compare case ignoring; 3. Use .compareTo() to compare strings in dictionary order, returning 0, negative or positive numbers; 4. Use .compareToIgnoreCase() to compare case ignoring; 5. Use Objects.equals() or safe call method to process null strings to avoid null pointer exceptions. In short, you should avoid using == for string content comparisons unless it is explicitly necessary to check whether the object is in phase.

Computed has a cache, and multiple accesses are not recalculated when the dependency remains unchanged, while methods are executed every time they are called; 2.computed is suitable for calculations based on responsive data. Methods are suitable for scenarios where parameters are required or frequent calls but the result does not depend on responsive data; 3.computed supports getters and setters, which can realize two-way synchronization of data, but methods are not supported; 4. Summary: Use computed first to improve performance, and use methods when passing parameters, performing operations or avoiding cache, following the principle of "if you can use computed, you don't use methods".

TestthePDFinanotherapptodetermineiftheissueiswiththefileorEdge.2.Enablethebuilt-inPDFviewerbyturningoff"AlwaysopenPDFfilesexternally"and"DownloadPDFfiles"inEdgesettings.3.Clearbrowsingdataincludingcookiesandcachedfilestoresolveren
