The last series was the SpringCloud starter series, and there will definitely be an advanced series in the future, but the current series is a multi-threaded series.

The importance of multithreading goes without saying. High concurrency is everywhere in our lives now. 618, double 11,12306, I don’t know how much concurrency that supports, I can’t say it too much, it’s very, very, very nice to these teams.

The focus of this article is to introduce you to the basic theory of processes and threading. The concepts covered are simple but very practical. Some concepts that may not be covered will be covered in the following articles.

process

Simple concept of a process

In the process model, all of a computer’s runnable software, usually including the operating system, is organized into sequential processes, or processes for short. A process is an example of a program being executed.

In plain English, a process is the project we started, or the applications we see when we open the task manager are all processes. A process consists primarily of the current values of program counters, registers, and variables.

Another concept that needs to be addressed here is daemons.

Processes that remain in the background are called daemons.

State of the process

The state of a process has three states: ready, running, and blocked. Ready is ready to run, but not yet, because the CPU is occupied by another process. Running is when the process is actually running on CPU; Blocking is basically paused, and the process can’t run unless something interferes with it.

Take nucleic acid for example, the medical staff is the CPU, the person who is doing nucleic acid is in the running state, the one who is queuing in the back is in the ready state, no one forces him to do not want to do it at all to queue is the blocking state.

It is important to note that these three states are interchangeable

  • Run blocking: This conversion occurs when the system finds that a process can no longer run, or when it believes that it is terminated.
  • Ready to run: Most of the time, these two transitions are one and the same, and are primarily determined by the system process scheduler, and the process is largely unaware of the schedule changes. When the system decides that the process has been using the CPU for a long time, the CPU’s time slice is reallocated according to a certain algorithm, and this is accompanied by a state transition.
  • Blocking Ready: A secondary transition occurs when a process is waiting for an external event to occur, typically, such as a large-scale nucleic acid test, a community door-to-door alert, and a queue.

The mode of communication between processes

There are 8 ways of communication between processes, but forget which book to see the concept of the 8, although there is an explanation on the Internet, but not too authoritative, did not write it, you friends temporarily first understand which 8 can be.

Named pipes, named pipes, advanced pipes, message queues, semaphore, signals, shared memory, and sockets.

thread

Basic concept of thread

For the thread, look up for a long time also did not have a clear concept, even in the “modern operating system” is a vague concept, mini process (called thread).

In general terms with Java programs, a program is a process, and then the main method is the main thread of that process, and then after that we create multiple threads.

We know that each process has an address space and a thread of control. Here the main thread is the thread of control.

State of thread

There are five states of a thread as opposed to a process.

  • New: New means that a New thread is created, but it has not been started. When a Thread is newly created, the JVM allocates memory and initializes the value of its member variables
  • Runnable: When a thread calls the start () method, the thread is in the ready state, waiting for the CPU slice. In the virtual machine, the Java VM creates a stack of method calls and program counters for it, waiting for the schedule to run
  • Running: As the name implies, the program is in the Running state. The corresponding code is that the thread in the ready state obtains the CPU and starts executing the thread execution body of the run () method, then the thread is in the Running state.
  • Blocked: Often heard of as a blocking state, a thread has for some reason relinquished CPU access, yielding a CPU timeslice and temporarily suspending its execution. This state is maintained until the thread enters the runnable state, and then it has a chance to get the CPU time slice again, thus returning to the running state again
  • Dead: The state at the end of the thread is Dead

For blocking states, there are three types:

  • Wait block: When a thread in a running thread calls the object.wait () method, the JVM puts the thread in the wait queue, causing the thread to block.
  • Synchronization block: When a running thread acquires a synchronization lock on an object, if the lock is held by another thread, the JVM will place the thread in the lock pool.
  • Other Blocking: The JVM puts a running Thread in a blocking state when it executes Thread.sleep(long ms), t.Join (), or when it makes an I/O request. When sleep() timeouts, join () waits for the thread to terminate or timeout, or I/O completes, the thread returns to the runnable state.

There are three ways to do a thread’s DEAD state:

  • The normal end is when the run () or call () method completes
  • The Exception ends, and the thread throws an uncaught Exception or Error while running
  • Calling the stop () method directly calls the stop () method of the thread to terminate the thread, but this method is prone to deadlock, so it is generally not recommended

How threads communicate with each other

There are three main ways to communicate between threads: shared memory, message passing and pipeline flow.

  • Shared memory: Java generally uses volatile shared memory
  • Messaging: Java uses methods such aswait/notify , joinMethods.
  • Pipe stream: The form of a pipe input/output stream

other

The difference between processes and threads

Multi – process is the operating system at the same time running multiple programs, multiple threads in the same process at the same time running multiple tasks.

Fundamental difference: A process is the basic unit of operating system resource allocation, while a thread is the basic unit of processor task scheduling and execution

Resource overhead: Each process has its own code and data space (program context), so switching between programs will have a large overhead. Threads can be regarded as lightweight processes. The same kind of threads share code and data space. Each thread has its own independent running stack and program counter (PC), and the overhead of switching between threads is low.

Inclusion relation: if there are multiple threads in a process, then the execution process is not a single line, but multiple lines (threads) to complete together; A thread is part of a process, so it is also called a light-weight or lightweight process.

Memory allocation: Threads of the same process share the address space and resources of the process, and the address space and resources of the process are independent of each other

Influential relationship: when a process crashes, it has no effect on other processes in protected mode, but when a thread crashes, the whole process dies. So multiple processes are more robust than multiple threads.

Execution process: Each individual process has an entry point for program execution, a sequential execution sequence, and a program exit. But the thread cannot execute independently, must depend on in the application program, by the application program provides multiple thread execution control, both can execute concurrently

Thread unsafe

There’s a quote in Java Concurrent Programming in Action

When multiple threads access a class, it is thread-safe if the class behaves correctly regardless of scheduling and alternating between the threads in the runtime environment, and if no additional synchronization is required and no other coordination is required by caller code.

In general, making code thread-safe means making sure that there are no errors in accessing the state of an object, which is generally referred to as data. But data is mostly shared and mutable.

As the name implies, shared means that the variable is accessible between threads. Variable means that the value of the data can be changed, not written.

resources

What is a resource, as defined in Modern Operating Systems

We call the objects that need to be used exclusively here resources. A resource can be a hardware device (such as a Blu-ray drive) or a set of information (such as a locked record in a database)…… In short,
A resource is anything that must be available, used, and released over time.

In my opinion, the resource is more like a lock. I don’t know what you think about it.

Parallelism and concurrency

Concurrency refers to the execution of tasks by multiple threads within the same time period, usually alternately; Parallel is when multiple threads are working at the same time.

At the operating system level, if multiple threads can be executed simultaneously by multiple CPUs, this is called parallelism. Concurrency is the execution of multiple threads being switched by a CPU according to an algorithm.

It is not easy to create, if you have help, welcome thumb up, collection and share!

The following is a personal public number, interested can pay attention to, perhaps is your treasure public number oh, basic 2,3 days 1 more technical articles!!