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

Scheduling and priority method of Java threads

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

Share

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

Today, I would like to share with you the relevant knowledge of Java thread scheduling and priority method. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

Because CPU is calculated frequently, billions of times per second, the time of CPU can be segmented in millisecond dimensions, and each segment is called a CPU time slice.

At present, the mainstream thread scheduling mode in the operating system is thread scheduling based on CPU time slice. The thread can execute the instruction only when it gets the CPU time slice, and the thread that does not get the time slice is in the ready state, waiting for the system to allocate the next CPU time slice. Because the time slice is very short and switches quickly between threads, it is characterized by many threads "executing simultaneously" or "concurrent execution".

At present, there are two kinds of thread scheduling models: time-sharing scheduling model and preemptive scheduling model.

(1) time-sharing scheduling model: the system distributes time slices of CPU evenly, and all threads take turns to occupy CPU, that is, all threads are "equal" in the allocation of time slices.

(2) preemptive scheduling model: the system allocates CPU time slices according to thread priority. High-priority threads assign CPU time slices first. If all ready threads have the same priority, then one will be randomly selected, and the high-priority thread will get more CPU time slices.

At present, most operating systems use the preemptive scheduling model for thread scheduling, and the thread management and scheduling of Java are entrusted to the operating system. Correspondingly, the thread scheduling of Java also uses the preemptive scheduling model, so the threads of Java have priority.

There is an instance property and two instance methods in the Thread class that are dedicated to thread priority-related operations. The member properties related to thread priority are:

/ / Save the priority of the Thread thread instance. Between 1 and 10, private int priority;// gets thread priority public final int getPriority () {/ /...} / / sets thread priority public final void setPriority (int priority) {/ /.}

The priority property of the Thread instance defaults to level 5, and the corresponding class constant is NORM_PRIORITY. The maximum priority value is 10 and the minimum value is 1. The three priority constants defined in the Thread class are as follows:

Public final static int MIN_PRIORITY = 1; public final static int NORM_PRIORITY = 5; public final static int MAX_PRIORITY = 10

The preemptive scheduling model is used for thread scheduling in Java. The higher the priority of the priority instance property, the more chances the thread will get the CPU time slice, but it is not absolute.

Example: 1. Define a thread execution body, which executes asynchronously: public class ThreadDemo extends Thread {private long num = 0; public long getNum () {return num;} @ Override public void run () {/ / thread execution body: endless loop for (int item0 switch exchange ibody +) {num++ } 2. Create 10 threads and set different thread priorities to execute the thread execution body: public class Main {public static void main (String [] args) throws InterruptedException {ThreadDemo [] threads = new ThreadDemo [10]; for (int iThreads)

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