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

Example Analysis of multithreaded method State and creation method of Java

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you the "Java multithreaded method state and creation method example analysis", the content is easy to understand, well-organized, hope to help you solve your doubts, the following let the editor lead you to study and learn "Java multithreaded method state and creation method example analysis" this article.

The five states of Java threads and their common methods (six states and timed_wating timeout waiting) 1. The five states of threads and their transitions

The five states of a thread are: creation state (New), ready state (Runnable), running state (Running), blocking state (Blocked), and dead state (Dead).

The following is a diagram of the relationship between the five states of the thread:

(1) New state: simply create a thread, there are three ways to create a thread, in my blog: thread creation, you can check it yourself!

(2) ready state: after creating a thread, call the start () method of the Thread class to start a thread, which means that the thread is in a ready state!

(3) running state: when the thread gets the CPU time, the thread enters the running state from the ready state!

(4) blocking state: after a thread enters the running state, it may enter the blocking state for a variety of reasons, such as calling the sleep () method to make the thread sleep, calling the wait () method to make the thread wait, calling the join () method, the suspend () method (which has now been deprecated! ) and blocking IO method.

(5) Dead state: the normal exit of the run () method puts the thread into the dead state, and when an exception is not caught and terminates the execution of the run () method will also enter the dead state!

two。 The method of setting or getting the thread name of a multithread

Because there may be multiple threads in a process, and the running state of the multithread is uncertain, that is, we do not know which thread is currently executing in the multithread. therefore, in multithreading operations, it is necessary to have a clear identifier to identify the information of the current thread object, which is often described by the name of the thread. Some methods are provided in the Thread class to set or get the thread name:

(1) set the thread name when creating a thread:

Public Thread (Runnable target,String name)

(2) the common method of setting thread name:

Public final synchronized void setName (String name)

(3) A common way to get a thread name:

Public final String getName ()

Demo:

Class MyThread implements Runnable {@ Override public void run () {for (int iTuno)

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