Multithreading basic three (heavy)

1. What are processes and threads?

Process: the smallest unit of the operating system for resource allocation

Thread: The smallest unit of the operating system to schedule the CPU. The threads have their own counters, stacks, and local variables, and they can also access shared memory variables.

Relationship: a process can create multiple threads

 

2. The relationship between the number of CPU cores and the number of threads?

Assuming core 2, logical processor 4

The number of threads that can be executed in parallel mainly depends on the logical processor, and 4 logical processors can process 4 threads in parallel.

But it can handle thousands of threads concurrently (the bottom layer is the time slice rotation mechanism RR time slice fast switching, each thread has the time slice to get the right to execute)

 

Three, thread data at the bottom

The CPU has 1 billion registers, and cache and other storage data

When the CPU executes a processing thread, the data needed by the thread will be stored in the register or cache. When the time slice runs out of rotation, the data of the thread will be stored in memory and other places.

When the time slice rotates to this thread again, the data will be taken from the memory to the register or cache, the thread will be processed, and the thread will continue to loop until the thread is destroyed and the data is recycled

 

Four, introduce the need to pay attention to the api of multithreading

Thread.run() This method can be executed after allocated to the CPU, where the business logic is implemented
Thread.start() Start the thread, let the thread be in the ready state, waiting for CPU allocation at any time
Thread.join() t1.join(), t1 thread jumps in the queue, and executes other threads until t1 is executed
Thread.join(long millis) t1.join(10), the t1 thread jumps in the queue for 10 milliseconds, and the threads execute alternately after the time is up
Thread.sleep(long millis) Let the thread go to sleep without releasing the lock resource and giving up the CPU, which is also blocking
Thread.yield() Make the current thread give up the CPU, the time cannot be specified, and the lock resource is not released
Thread.setDaemon(boolean on) setEDaemon(true) When true is passed in, it is set as a daemon thread, and GC is a daemon thread
Thread.interrupt() Interrupt the thread, but not to stop the thread running, just an interrupt status number is set to true, you can do subsequent processing
Thread.interrupted() Test whether the current thread is already in the interrupted state, and has the function of clearing the state after execution. The internal implementation is the isInterrupted() of the current thread called, the interruption returns true otherwise false
Thread.isInterrupted() Test whether the thread Thread object is already in the interrupted state, but does not clear the state flag
Thread.stop() The thread stops the thread and releases the lock by throwing an exception, which will cause data insecurity and has been eliminated (try to use Thread.interrupt, etc.)
Thread.suspend() If the thread stops the thread, it will cause a deadlock problem and has been eliminated (try to use Thread.interrupt, etc.)
Object.wait() Make the thread wait, release the lock, give up the CPU, block, it can be used only in synchronized, otherwise an exception will be thrown
Object.notify() Wake up a single thread that is waiting on this object monitor
Object.notifyAll() Wake up all threads waiting on this object monitor
LockSupport.park() Blocking threads, the bottom layer is Unsafe, operated by os, and Lock is commonly used
LockSupport.unpark() Wake up the thread, the bottom layer is Unsafe, operated by os, and Lock is commonly used

 

Five, thread status

 

Six, thread type

User-level threads (ULT), kernel-level threads (KLT)

JVM uses kernel-level threads

User thread (ULT): Refers to the thread that does not require kernel support and is implemented in the user program. It does not depend on the operating system core. The application process uses the thread library to provide functions for creating, synchronizing, scheduling and managing threads to control the user thread. In addition, the user thread is created and managed by the application process using the thread library, and does not depend on the operating system core. No user mode/core mode switching is required, and the speed is fast. The operating system kernel does not know that it is created and managed by the application process using the thread library. Blocking will cause the entire process (including all its threads) to block

Kernel thread (KLT): All thread management operations are completed by the operating system kernel. The state and context information saved by the kernel. When a thread executes a system call that causes blocking, the kernel can schedule other threads of the process to execute. On a multi-processor system, the kernel can dispatch multiple threads belonging to the same process to run on multiple processors to improve the parallelism of process execution. Since the kernel is required to complete thread creation, scheduling, and management, these operations are much slower than user-level threads, but still faster than process creation and management operations.

The test code is as follows (1000 threads are running and all sleep for 10s, so you can see in the task manager that the number of threads will increase by 1000, indicating that it has also established threads in the operating system)

        for (int i = 0; i < 1000; i++) {
            new Thread() {
                @Override
                public void run() {
                    try {
                        sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        }

 

 


 

Guess you like

Origin blog.csdn.net/qq_41055045/article/details/102610377
Recommended