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

How to understand the scheduling mechanism of Java

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly introduces the relevant knowledge of "how to understand the scheduling mechanism of Java". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to understand the scheduling mechanism of Java" can help you solve the problem.

The relationship between the number of CPU cores, the number of threads, the number of CPU, the number of cores and the number of threads:

Number of CPU: refers to the number of cores physically, that is, the number of cores on hardware

Core number: it is logical and simply understood as the core number simulated logically.

Number of threads: the number of programs that devices can execute in parallel at the same time, number of threads = number of cpu * cores

Number of Java threads and CPU multithreading concepts:

A single Java thread can only execute a single CPU program, that is, a thread, at the same time.

A single thread can only execute in a single CPU thread at the same time

The thread is the smallest scheduling unit of the operating system, and the process is the smallest unit of resource allocation (such as memory).

All threads in Java are in the JVM process, and CPU dispatches threads in the process.

Java multithreading is not called multithreading because the number of CPU threads is more than one. When the number of Java threads is greater than the number of CPU threads, the operating system uses time slice mechanism and thread scheduling algorithm to switch threads frequently.

Does the thread release CPU when IO is blocked?

When a thread is in an IO operation, the thread is blocked and the thread changes from the running state to the waiting state. At this point, CPU will switch context to handle other programs; when the IO operation is completed, CPU will receive an interrupt signal from the hard disk, and the thread that CPU is executing will be interrupted and return to the ready queue. On the other hand, the thread that was previously waiting because of iUnip O will return to the ready queue again with the completion of iUnip O, and CPU may choose him to execute at this time.

The concept of concurrency and parallelism in JAVA

Parallel: two or more events occur at the same time and CPU executes at the same time; concurrency: two or more events occur in the same time period and CPU executes alternately

Can JAVA threads run on multiple cores at the same time? (thinking)

The operating system is based on thread scheduling, and different threads in the JAVA process may run in parallel on different cores at the same time.

A thread is the smallest unit of scheduling, while a process is the smallest unit of resource allocation (such as memory).

Time slice rotation mechanism

Time slice rotation method (Round-Robin,RR):

According to the FIFO principle, queue is formed (ready queue). When scheduling, the CPU is assigned to the queue leader process to execute a time slice (called: time slice), which is usually of the order of 10-100ms. When the executed time slice is used up, a clock interrupt request is issued by the timer, and the scheduler stops the execution of the process accordingly and queues it to the end of the queue. Then reassign the CPU to the queue leader process of the current queue, again and again.

The size of the time slice depends on:

Response time requirements of the system

Number of processes in the ready queue

The processing capacity of the system

Process scheduling

In the system using this algorithm, the program ready queue is often sorted according to the time of arrival of the process. The process scheduler always selects the first process in the ready queue, that is, it is scheduled on a first-come-first-served basis, but only one time slice is used once the process occupies the processor. After using a time slice, the process has not finished running, it must release the processor to the next ready process, and the preempted process returns to the end of the ready queue to be re-queued to run again.

The processor can only handle one task at a time. When the processor is dealing with multitasking, it depends on the timing of the request, and if the time is consistent, it is necessary to make a prediction. After picking a task, it takes several steps to complete, some of which require the involvement of the processor and some do not (such as the stored procedures of the disk controller). When the processor is not needed, this part of the time is allocated to other processes. The original process will be in a waiting period of time. After careful allocation of time, macroscopically, it is like multiple tasks running together, but microscopically, there is a sequence, that is, the rotation of time slices.

Realization thought

The basic idea of the time slice rotation algorithm is that the system arranges all the ready processes into a queue according to the principle of the first-come-first-served algorithm. During each scheduling, the system assigns the processor to the queue head process and lets it execute a time slice. When the executed time slice runs out, a timer issues a clock interrupt request, according to which the scheduler stops the process, sends it to the end of the ready queue, and allocates the processor to the new queue head process in the ready queue. At the same time, let it also execute a time slice.

Java scheduling mechanism

All Java virtual machines have a thread scheduler that determines which thread is running at which time. There are two main types: preemptive thread scheduler and collaborative thread scheduler.

Preemptive thread scheduling: each thread may have its own priority, but priority and does not mean that high-priority threads will be scheduled, but randomly selected by CPU, the so-called preemptive thread scheduling, that is, a thread in the execution of its own task, although the task is not finished, but CPU will force it to pause, let other threads occupy the right to use CPU.

Collaborative thread scheduling: each thread can have its own priority, but priority does not mean that high-priority threads must be scheduled first, but by the timing of cpu, the so-called collaborative thread scheduling, that is, a thread is not allowed to be interrupted when executing its own task, and the occupation of cpu will not be released until the current thread finishes executing the task, so that other threads can preempt the cpu.

Comparison of the two:

Preemptive thread scheduling is not easy to occur hunger, and it is not easy to affect the execution of the whole process because of the problem of one thread, but its frequent blocking and scheduling will result in a waste of system resources. Collaborative thread scheduling is easy to starve other threads in the whole process because of the problem of one thread.

Summary:

Java adopts preemptive thread scheduling mechanism in scheduling mechanism.

Java threads are collaborative among multiple threads while they are running.

This is the end of the content about "how to understand the scheduling mechanism of Java". Thank you for your reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report