preface
Ask a few questions
Why do we call start() of the thread instead of run()?
Call the thread’s start() and the thread will execute immediately?
How much time does the operating system allocate to threads?
All right! Let’s get started….
Process definition
Before we talk about threads, let’s take a quick look at processes.
The term “process” was first introduced in the early 1960s in the MULTICS system of THE Massachusetts Institute of Technology and the CTSS/360 system of IBM. Process is one of the most basic and important concepts in operating system. Master this concept for understanding the essence of the operating system, for analysis, design operating system has very important significance. So far, however, the concept of process has not had a very precise and unified definition. There are many different definitions of “process” from different perspectives. Here are some definitions from operating system pundits.
(1) A rule of behavior is called a program, and the activity that occurs when the program is executed on the processor is called a process (Dijkstra).
(2) Processes are computes that can be executed concurrently with other computes (Madnick and Donowan).
(3) A process is an activity that occurs when a program and its data are executed sequentially on a processor (A.C.Shaw).
(4) Process is the running process of the program on a data set, and is an independent unit of the system for resource allocation and scheduling (Peter Denning).
What is a thread?
Because a thread is an entity in a process, the thread itself does not exist independently. Process is a code in the data set of a running activity, is the system for resource allocation and scheduling of the basic unit, thread is a process of execution path, a process at least one thread, multiple threads in the process share the process of resources.
In early operating systems, there was no concept of a thread. A process was the smallest unit that could own resources and run independently, and the smallest unit of program execution.
Benefits of introducing threads
(1) It takes less time to create a new thread (as well as to end it). Thread creation requires no additional allocation of resources, so thread creation is faster than process creation, and the system costs less.
(2) It takes less time to switch between two threads.
(3) Because threads in the same process share memory and files, there is no need to call the kernel for communication between threads, so there is no need for additional communication mechanism, so that communication is easier and information transmission speed is fast.
(4) Thread can be independently executed, can make full use of and play processor and peripheral device parallel work ability.
Why is a call to start() not immediately executed by a thread
@override public void run() {super.run(); System.out.println("running...." ); } }.start(); }Copy the code
After the start method, the thread does not execute immediately. Instead, it is in the ready state. This state means that the thread has acquired resources other than CPU resources and will not be running until it has acquired CPU resources. Once the run method completes, the thread is terminated.
Explain thread state first, then the size of cup calls and allocated time slices.
The five states of threads are as follows:
New state: After a Thread object is created using the new keyword and the Thread class or its subclasses, the Thread object is in the new state. It remains in this state until the program starts () thread.
Ready: When a thread object calls the start() method, the thread enters the ready state. A thread in a ready state is in a ready queue, waiting to be scheduled by the thread scheduler in the JVM.
Running state: If a ready thread acquires CPU resources, run() can be executed and the thread is running. The running thread is the most complex, and can be blocked, ready, or dead.
Blocked: If a thread executes methods such as sleep or suspend and loses resources, it enters the blocked state from running. You can re-enter the ready state when it is time to sleep or after obtaining device resources. It can be divided into three categories:
Wait to block: A running thread executes wait() to put the thread into the wait to block state.
Synchronized blocking: a thread fails to acquire a synchronized lock (because it is occupied by another thread).
Other blocking: The thread is blocked when an I/O request is made by calling the thread’s sleep() or join(). When the sleep() state times out, join() waits for the thread to terminate or time out, or the I/O process completes, and the thread reverts to the ready state.
Dead: When a running thread completes a task or other termination conditions occur, the thread switches to the terminated state.
On the key
Multicore is really parallel execution, and the number of parallel threads depends on how many processing cores there are. For example, i7 has 4 cores and 8 threads.
If you look at the number of threads and processes, even on a multi-core CPU you can see that our computers tend to run hundreds of processes and thousands of threads. It is also impossible to run thousands of threads at the same time, so the operating system uses a time-allocation mechanism where each process is given a time slice of 20-200ms to run. When the time slice is used up, it switches to the next process, records the process information in the process management block PCB, and then enters the ready state queue, waiting for the next allocation of time slice. Give me an example! Fuck the old actors.
Single-core operating system is used here:
Compare processes to people
For example, if you have six people in a bathroom, why is it a bathroom? Because multiple processes are executed alternately at the micro level. So 6 people take turns to use a bathroom, this is called multi-thread distribution, because the system multi-process is time slice, that is, each person can only use a moment, such as here is 30 seconds, you have to change to another. What if it is half pulled at this time? At this point, you need to clamp off (this is the system interrupt mechanism \color{blue}{system interrupt mechanism} system interrupt mechanism, the process management block PCB, will save the person pulled there, waiting for the next bathroom). And then the next, and so on.
In fact, this is a waste of resources, if three of them are not in a hurry or have no need, it is also a waste of resources to go in. So there’s optimization, no need, wait outside. (this is the blocking state \color{blue}{blocking state} blocked state), put the three people in the blocking queue, then the pressure of the bathroom has been reduced by half, and it is time to go to the bathroom happily. Wait for them to line up again, and then….. The blocking state becomes ready, queuing, waiting for the system to allocate time slice – execution state.
Look at the map
Originally wanted to draw a hand, drew several times, forget it, soul painter, I forgot I am an amateur photographer, direct picture processing is not on the line.
Adding the concept of multithreading, the process management block PCB has also been extended to include information about each thread, among other changes to support multithreading. In modern systems, a process can have multiple threads. Multicore computers can execute multiple threads at the same time, which greatly increases efficiency. (Cup is full!) Switching processes is expensive, while switching threads is cheap. For example, if you put three people in the toilet, i.e. by the toilet, the process is more efficient without opening and closing doors.
thank you
I’m sure you all know the answer.
reference
The Beauty of Concurrent Programming in Java
Operating System Concepts, ninth edition
Operating Systems, fourth edition
My knowledge is limited, if there is any wrong description, I would like to correct.
Look at thisLooks like you owe me a thumbs-up. Thank you. Your praise is like the warm sun in winter, warming the heart.