天天看点

java 并发线程_Java并发性:线程生命周期 线程状态 (Thread states) 结论 (Conclusion)

java 并发线程

In the Java language, multithreading is driven by the core concept of a Thread. During their lifecycle, threads go through various stages.

在Java语言中,多线程由线程的核心概念驱动。 在线程的生命周期中,它会经历各个阶段。

Understanding Thread Life Cycle and the Thread States are very important when you are working with multithreaded programming.

当您使用多线程编程时,了解线程生命周期和线程状态非常重要。

To understand thread states and when the thread is moving between these states we will use a diagram:

为了理解线程状态以及线程在这些状态之间移动的时间,我们将使用一个图表:

java 并发线程_Java并发性:线程生命周期 线程状态 (Thread states) 结论 (Conclusion)

On this diagram, you can see all the possible states of a Thread and conditions when these states are changing. To make it more clear we will go through all these states. But before let's check what each state means.

在此图上,您可以查看线程的所有可能状态以及这些状态更改时的条件。 更清楚地说,我们将经历所有这些状态。 但是在检查每个状态意味着什么之前。

线程状态 (Thread states)

The java.lang.Thread class contains a static State enum — which defines its potential states. The thread can only be in one of these states at a time:

java.lang.Thread类包含一个静态 枚举 —定义其潜在状态。 线程一次只能处于以下状态之一:

  1. NEW — a newly created thread that has not yet started the execution

    NEW —尚未开始执行的新创建的线程

  2. RUNNABLE — either running or ready for execution but it’s waiting for resource allocation

    RUNNABLE —正在运行或准备执行,但正在等待资源分配

  3. WAITING — waiting for some other thread to perform a particular action without any time limit

    等待中—等待其他线程执行特定操作而没有任何时间限制

  4. TIMED_WAITING — waiting for some other thread to perform a specific action for a specified period

    TIMED_WAITING —等待其他线程在指定时间段内执行特定操作

  5. BLOCKED — waiting to acquire a lock to enter or re-enter a synchronized block/method

    已阻止-等待获取锁以进入或重新进入同步块/方法

  6. TERMINATED — has completed its execution

    TERMINATED-已完成执行

Now we can go through all these states.

现在我们可以经历所有这些状态。

NEW

A NEW Thread is a thread that’s been created but not yet started. It remains in this state until we start it using the start() method.

NEW Thread是已创建但尚未启动的线程。 它将保持这种状态,直到我们使用start()方法启动它为止。

The following code snippet shows a newly created thread that’s in the NEW state:

以下代码段显示了一个新创建的处于NEW状态的线程:

Thread t = new MyThread();
           

RUNNABLE

可运行

When we’ve created a new thread and called the start() method on that, it’s moved from NEW to RUNNABLE state. Threads in this state are either running or ready to run, but they’re waiting for resource allocation from the system.

当我们创建了一个新线程并在其上调用start()方法时,它已从NEW变为RUNNABLE状态。 处于此状态的线程正在运行或可以运行,但是它们正在等待系统分配资源。

In a multi-threaded environment, the Thread-Scheduler (which is part of JVM) allocates a fixed amount of time to each thread. So it runs for a particular amount of time, then passes the control to other RUNNABLE threads.

在多线程环境中,线程调度程序(JVM的一部分)为每个线程分配固定的时间量。 因此它运行特定的时间,然后将控件传递给其他RUNNABLE线程。

The following code snippet shows a newly created thread that’s in the RUNNABLE state:

以下代码片段显示了一个新创建的处于RUNNABLE状态的线程:

Thread t = new MyThread();
t.start();
           

WAITING

等候

A thread is in WAITING state when it’s waiting for some other thread to perform a particular action. According to JavaDocs, any thread can enter this state by calling any one of the following three methods:

等待其他线程执行特定操作时,该线程处于WAITING状态。 根据JavaDocs的说法 ,任何线程都可以通过调用以下三种方法之一来进入此状态:

  1. object.wait()

    object.wait()

  2. thread.join() or

    thread.join()或

  3. LockSupport.park()

    LockSupport.park()

Here is an example:

这是一个例子:

What was happened:

发生了什么事:

  1. We’ve created and started a thread — threadA

    我们创建并启动了一个线程— threadA

  2. threadA creates a threadB and starts it

    threadA创建一个threadB并启动它

  3. Inside threadA we called threadB.join(), this puts threadA in WAITING state until threadB has finished execution

    在threadA内部,我们称为threadB.join() ,这将线程A置于等待状态,直到线程B完成执行

TIMED WAITING

定时等待

A thread is in TIMED_WAITING state when it’s waiting for another thread to perform a particular action within a specified amount of time.

当一个线程正在等待另一个线程在指定的时间内执行特定操作时,该线程处于TIMED_WAITING状态。

According to JavaDocs, there are five ways to put a thread on TIMED_WAITING state:

根据JavaDocs的介绍 ,有五种方法可以将线程置于TIMED_WAITING状态:

  1. thread.sleep(long millis)

    thread.sleep(长毫秒)

  2. wait(int timeout) or wait(int timeout, int nanos)

    wait(int timeout)或wait(int timeout,int nanos)

  3. thread.join(long millis)

    thread.join(长毫秒 )

  4. LockSupport.parkNanos

    LockSupport.parkNanos

  5. LockSupport.parkUntil

    LockSupport.parkUntil

What was happened:

发生了什么事:

  1. We’ve created and started a threadA

    我们创建并启动了一个线程

  2. Move to sleep main thread

    进入睡眠主线程

  3. threadA start to processing

    线程开始处理

  4. Method sleep is called — threadA in TIMED_WAITING

    调用方法sleep — TIMED_WAITING中的 threadA

BLOCKED

已封锁

A thread is in the BLOCKED state when it’s currently not eligible to run. It enters this state when it is waiting for a lock and is trying to access a section of code that is locked by some other thread.

当前不符合运行条件的线程处于“ 阻塞”状态。 当它等待锁并尝试访问被某个其他线程锁定的一段代码时,它将进入此状态。

Here is an example:

这是一个例子:

What was happened:

发生了什么事:

  1. We’ve created two different threads — threadA and threadB

    我们创建了两个不同的线程-threadA和threadB

  2. One of the threads entered synchronized infinityMethod() method(there is no guaranty that threadA will run this method first) this means that only one thread can access it; all other threads that try to access this method will be blocked from the further execution until the current one will finish the processing

    输入的线程之一同步了infinityMethod()方法(没有保证threadA首先运行该方法),这意味着只有一个线程可以访问它; 尝试访问此方法的所有其他线程将被阻止进一步执行,直到当前线程将完成处理为止

  3. The second thread will try to access infinityMethod() but this method is in use by the first thread — the second thread will be BLOCKED

    第二个线程将尝试访问infinityMethod(),但第一个线程正在使用此方法-第二个线程将被阻塞

TERMINATED

已终止

This is the state of a dead thread. It’s in the TERMINATED state when it has either finished execution or was terminated abnormally.

这是死线程的状态。 完成执行或异常终止时,它处于TERMINATED状态。

Here is an example:

这是一个例子:

Here, while we’ve started thread t1, the very next statement Thread.sleep(1000) gives enough time for t1 to complete and so this program gives us the output as:

在这里,当我们启动线程t1时 ,下一条语句Thread.sleep(1000)为t1完成提供了足够的时间,因此该程序将输出显示为:

What was happened:

发生了什么事:

  1. We’ve created and started a threadA

    我们创建并启动了一个线程

  2. Move to sleep main thread

    进入睡眠主线程

  3. threadA start to processing

    线程开始处理

  4. threadA is finished — became in state TERMINATED

    线程A完成-进入状态TERMINATED

结论 (Conclusion)

In this article, we learned thread states and how to switch between all states.

在本文中,我们学习了线程状态以及如何在所有状态之间切换。

In the next article, we will check How to create a thread. Stay tuned.

在下一篇文章中,我们将检查如何创建线程 。 敬请关注。

Found this article useful? Follow me (Dmytro Timchenko) on Medium and check out my other articles below! Please 👏 this article to share it!

觉得这篇文章有用吗? 在Medium上关注我( Dmytro Timchenko ),并在下面查看我的其他文章! 请👏这篇文章分享!

Java Concurrency

Java并发

Spring RESTful: Hibernate

SpringRESTful:Hibernate

Spring RESTful: Docker, PostgreSQL, Liquibase

SpringRESTful:Docker,PostgreSQL,Liquibase

翻译自: https://medium.com/javarevisited/java-concurrency-thread-life-cycle-4869432474b

java 并发线程