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

What are the thread states in Java multithreading

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

Share

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

This article mainly shows you "what is the thread state in Java multithreading", which is easy to understand and clear. I hope it can help you solve your doubts. Let me lead you to study and learn "what is the thread state in Java multithreading".

Thread state

Five states: new, ready, running, dead, blocking

Stop the thread

The stop () and destroy () methods provided by JDK are not recommended [deprecated]

It is recommended that the thread stop itself.

It is recommended to use a flag bit to terminate the variable, and to flag=false, the thread is terminated.

Public class StopDemo implements Runnable {/ / set a flag bit boolean flag = true; @ Override public void run () {/ / the thread body uses the flag while (flag) {System.out.println ("runing....") }} / / set a public method to stop the thread, and convert the flag bit public void stop () {this.flag = false;}} thread hibernation

Sleep (time) specifies the number of milliseconds blocked by the current thread

Abnormal Interrupted Exception exists in sleep

The thread enters the ready state after the sleep time is reached

Sleep can simulate network delay, countdown, etc.

Every object has a lock, and sleep will not release the lock (key note)

Simulate network latency (magnify the occurrence of problems)

Grab tickets:

Public class SleepDemo implements Runnable {/ / votes private int tickeNum = 10; @ Override public void run () {while (true) {if (tickeNum {for (int I = 0; I)

< 5; i++) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("==========="); } }); // 观察状态 new System.out.println(thread.getState()); // 启动后 run thread.start(); System.out.println(thread.getState()); // 只要线程不终止,就一直输出状态 while (thread.getState() != Thread.State.TERMINATED) { Thread.sleep(500); System.out.println(thread.getState()); } }}

Thread priority

Java provides a thread scheduler to monitor all threads in the program that enter the ready state after startup. The thread scheduler determines which thread should be scheduled for execution according to priority.

The priority of a thread is expressed as a number, ranging from 1 to 10

Thread.MIN PRIORITY = 1

Thread.MAX PRIORITY = 10

Thread.NORM PRIORITY = 5

Change or obtain priority in the following ways

GetPriority (), setPriority (int xxx)

It is not certain that the thread with high priority will run first. Low priority only means that the probability of getting the schedule is low. It is not that the thread with low priority will not be called. It mainly depends on CPU scheduling, and performance inversion may occur.

Daemon thread

Threads are divided into user threads and daemon threads

The virtual machine must ensure that the user thread is finished

The virtual machine does not have to wait for the daemon thread to finish execution

For example, the background records operation logs, monitors memory, garbage collection, some waiting mechanisms, and so on.

These are all the contents of the article "what are the thread states in Java multithreading?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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