This paper has participated in theProject Digg”To win a creative gift package and challenge for creative incentive money. @[TOC]
🚩 1. Understand processes and threads
🥇1.1 What is a process
A “process” is the “process” by which a computer performs a job.
A running program on a device is a process. For example, the QQ you open is a process, and the wechat you are chatting with is also a process. Processes are the basic unit of resource allocation in the system.
When we open the task Manager, we can see the processes running in the current operating system.In order for a process to really run, it needs to allocate some system hardware resources to the process. These resources include: CPU: For example, in my computer, task manager occupies 11% of THE CPU, while QQ occupies 0.3%. Memory: The task manager takes up 45.7MB. Microsoft Edge used 320.0MB of disk: QQ used 0.2MB/ SEC. Network bandwidth….
Take an example: I am the monitor, and the teacher wants me to organize an activity. If I want to organize this activity, I need to apply to the teacher for some activity funds and personnel. I’m the process here. The teacher is the CPU. When I organized the activity, I applied to the teacher for funds and people. You’re asking for some resources to be allocated. With funds, someone can do the activity well. <html
Process management
Organization of processes:
Use a data structure to organize. A common practice is to use a two-way linked list. When you look at the list of processes, you’re essentially walking through the linked list of organizational processes in the operating system kernel and displaying these attributes for each process. Creating a process essentially creates a PCB object and adds that object to the kernel’s linked list. Destroying a process essentially removes the PCB object from the kernel list.
Process description:
PCB describes the process. The PCB is actually a very large structure with many attributes, such as PID (second column of the table below), memory Pointers, file descriptor tables, process status, context, priority, billing information, and so on. PID: Identifies a process. The PID of each process on a machine is unique at the same time. Memory pointer: describes the range of memory used by this process. File description Attached: describes which files are open for this process
Process scheduling
When it comes to processes, it involves the scheduling of processes. Just now, you can see that there are many processes on my computer. Although only 5 processes are displayed in the application, the background still helps me to run 87 processes. I believe your computer must not have so much CPU bar. The number of cpus is less than the number of processes, but I need those processes to “execute simultaneously.” Our system is a multi-tasking system. The multi-task system is actually based on the process scheduling mechanism to complete.
Parallel execution
For example, suppose there is Xiao Zhang, who is very good-looking and has many pursuers in school. According to the standard of normal man, I can associate with only one girl at the same time ~ that xiao Zhang classmate has no way to do at the same time with many girls? Xiao Zhang thought long and hard, the final decision! Make a schedule!! Monday morning: have breakfast with Girl A Tuesday afternoon: go shopping with girl B Wednesday evening: go to the movies with Girl C As long as Xiao Zhang arranges his schedule, the three girls will not know the existence of the other two. From a macro point of view, Xiao Zhang dated three girls at the same time. On a micro level, Xiao Zhang was dating only one girl at a time. Good man.
This is how the operating system manages processes when you switch to a computer. It’s just that in real life, cpus run too fast for us to notice. We feel as if the CPU is running so many processes at once.
Priority of the process: who is given priority when scheduling the context of the process: save register information to memory. Record which instruction was run to last time, so that the next schedule can be conveniently continued from this position. Can be understood as a stand-alone game of the archive, read files. Process accounting information: Records how long a process has been running on the CPU to help determine whether the process should continue or be scheduled.
🥇1.2 Get to know threads
Why do we need threads?
We introduced processes to enable concurrent programming. In order to run multiple applications at the same time, there is still some room for improvement, although multi-processes are already capable of concurrent processes. Creating processes, destroying processes, and scheduling processes are just too expensive. To do this, threads were introduced.
Threads threads are also called “lightweight processes” on some systems. Why is it light weight? Creating threads is more efficient than creating processes; Destroying threads is more efficient than destroying processes; Scheduling threads is more efficient than scheduling processes; Because the creating thread does not request resources, the destroying thread does not need to release resources. Threads are created directly within the process, sharing previous resources. Threads and bases are contained. A process can contain multiple threads or one thread. When a process is created, resources are allocated and threads are created within the process, so that the threads share the same resources as the previous process.
🥇1.3 Differences and relationships between processes and threads (interview questions)
1. Process is the basic unit of operating system resource allocation, thread is the basic unit of operating system scheduling execution. 2, the process contains threads, a process can contain multiple threads, can also contain a thread. 3. Each process has its own memory space (virtual address space), which is shared by multiple threads of the same process.
Several ways to create threads
Create a custom class that inherits Thread and overrides run
/** * Thread is a Thread class in the Java standard library. The common way to do this is to define a class that inherits Thread and override the run method
public class threadDemo1 {
public static void main (String[] args) {
Thread t=new Thread ();
The start method creates a thread in the operating system.t.start (); }}class MyThread extends Thread{
@Override
public void run(a){
System.out.println ("Inherit Thread class to create Thread"); }}Copy the code
2, implement Runable interface, rewrite the run method
public class threadDemo2 {
public static void main (String[] args) {
Thread T=new Thread (newmyRunable ()); T.start (); }}class myRunable implements Runnable{
@Override
public void run(a){
System.out.println ("Implement Runbale interface, override run"); }}Copy the code
Thread overrides the run method using an anonymous inner class
public static void main (String[] args) {
Thread t=new Thread (){
@Override
public void run(a){
System.out.println ("Anonymous inner class"); }}; t.start (); }Copy the code
Implement Runable, override the run method, and use anonymous inner classes
public static void main (String[] args) {
Thread t=new Thread (new Runnable () {
@Override
public void run (a) {
System.out.println ("Implement Runable, override Run, use anonymous inner classes."); }}); t.start (); }Copy the code
5. Use lambda expressions
//
Thread t3 = new Thread(() -> System.out.println("Create Thread subclass objects using anonymous classes"));
Thread t4 = new Thread(() -> {
System.out.println("Create Thread subclass objects using anonymous classes");
});
Copy the code
So that’s our first glimpse of threads. There will be further updates later.