Number of CPU cores, number of threads

Number of cpus, number of cores, number of threads:

  1. Number of cpus: refers to the number of physical cores, that is, the number of hardware cores.
  2. Core number: logical, simply understood as the number of cores simulated logically;
  3. Number of threads: indicates the number of programs that can be concurrently executed by the device at the same time. Number of threads = number of cpus x number of cores.

CPU threads and Java multithreading concepts:

  1. A single CPU thread can only execute a single Java program at a time, that is, one thread
  2. A single thread can only execute in a single CPU thread at a time
  3. Threads are the smallest unit of scheduling in an operating system, and processes are the smallest unit of allocation of resources (e.g., memory)
  4. All Threads in Java In a JVM process, the CPU schedules the threads in the process
  5. Java multithreading is not called multithreading because the number of CPU threads is multiple. 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 frequently switch threads.

Does the thread free the CPU when I/O blocks?

When a thread is in an IO operation, the thread is blocked and changes from the running state to the wait state. The CPU does a context switch to process other programs. When the I/O operation is complete, the CPU receives an interrupt signal from the hard disk, and the thread that the CPU is executing is interrupted and returns to the ready queue. A thread that was waiting for I/O returns to the ready queue as the I/O completes, and the CPU may select it to execute.

Concepts of concurrency and parallelism in JAVA

  • Parallel: Two or more events occur at the same point in time and are executed simultaneously by the CPU.
  • Concurrency: Two or more events occur at the same time and the cpus execute them alternately.

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

Operating systems are based on thread scheduling, and different threads in a JAVA process may be running in parallel on different cores at the same time.

  • Threads are the smallest unit of scheduling, while processes are the smallest unit of resource allocation (e.g., memory).

Time slice rotation mechanism

Round-robin (RR) : According to the first-in, first-out principle, a queue is formed (ready queue). During scheduling, cpus are allocated to the first queue process for a period of time (called: Time slice), the time slice is usually 10-100ms. When the execution time slice is used up, the timer will send out a clock interrupt request, and the scheduler will stop the execution of the process according to this, and put it to the end of the queue, and then reallocate THE CPU to the first queue process of the current queue, and so on.

Time slice size depends on:

  1. System response time requirements
  2. The number of processes in the ready queue
  3. The processing power of the system

Process scheduling

In systems using this algorithm, the program ready queue is usually sorted by the time the process arrives. The process scheduler always selects the first process in the ready queue, that is, scheduled on a first-come, first-served basis, but uses only one time slice once the process occupies the processor. After using a timeslice, if a 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 requeue to run again.

A processor can only handle one task at a time. When multitasking, the processor looks at the chronological order of requests and makes predictions if The Times are consistent. Once a task is selected, several steps are required to complete it, some of which require processor involvement and some of which do not (for example, the disk controller’s stored procedures). When no processor is needed, this time is allocated to other processes. The original process is waiting for a period of time. After careful allocation of time, the macro is like multiple tasks running together, but the micro is sequential, that is, time slice rotation.

Implement ideas

The basic idea of time slice rotation algorithm is that the system arranges all ready processes into a queue according to the principle of first come, first served algorithm. Each time when scheduling, the system assigns the processor to the first queue process and lets it execute a time slice. When the time slice runs out, a timer sends out a clock interrupt request, and the scheduler stops the process according to this request, sends it to the end of the ready queue, and then assigns the processor to the new queue head process in the ready queue, and lets it also execute a time slice

Java scheduling Mechanism

All Java virtual machines have a thread scheduler that determines which thread to run at that time. There are two main types: preemptive thread scheduler and cooperative thread scheduler.

- ** Preemptive thread scheduling ** : Each thread may have its own priority, but the priority and it doesn't mean higher-priority thread will be scheduled, but by the CPU random selection, so-called preemptive thread scheduling, that is to say when a thread in the performance of their tasks, although the task is not performed, but the CPU will force it to suspend, let the other threads occupy CPU usage rights. - ** Collaborative thread scheduling ** : Each thread has its own priorities, but does not mean that higher priority thread priority must be the first to scheduling, but by the CPU timing, so-called collaborative thread scheduling, that is to say when a thread in the performance of their tasks, are not allowed to be interrupted, such as the current thread must be will be will only be released after completion of task execution on the CPU's possession, Only other threads can preempt the CPU.Copy the code

Comparison between the two:

Preemptive thread scheduling is not easy to starve, 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 cause the waste of system resources. Collaborative thread scheduling can easily starve other threads in the entire process because of a thread failure.

Conclusion:

Java uses preemptive thread scheduling mechanism in scheduling mechanism. Java threads work cooperatively with multiple threads during execution.