Relationship among process, thread and co process
- Process: in essence, it is an independently executed program. Process is the basic concept of resource allocation and scheduling by the operating system. The operating system is an independent unit for resource allocation and scheduling.
- Thread: it is the smallest unit that the operating system can schedule operations. It is included in the process and is the actual operation unit in the process. Multiple threads can be concurrent in a process. Each thread performs different tasks, and the switching is controlled by the system.
- Coroutine: also known as micro thread, it is a lightweight thread in user mode. Unlike threads and processes, coroutine requires context switching on the system kernel. The context switching of coroutine is determined by the user and has its own context. Therefore, it is a lightweight thread, also known as user level thread, which is called coroutine. A thread can have multiple coroutines, Thread processes are synchronous, while coprocesses are asynchronous
The native syntax of Java does not implement the co process. At present, python, Lua, GO and other languages support it - Relationship: a process can have multiple threads, which allows the computer to run two or more programs at the same time. The thread is the smallest execution unit of the process. The scheduling of CPU switches the process and thread. After the process and thread are more, the scheduling will consume a lot of CPU, and the thread running on CPU is actually thread, and the thread can correspond to many cooperations.
Advantages and disadvantages of concurrency for multithreading
- advantage:
Very fast context switching without the context switching of the system kernel to reduce the overhead
A single thread can achieve high concurrency, and a single core CPU can support tens of thousands of collaborative processes
Since there is only one thread and there is no conflict of writing variables at the same time, there is no need to lock the shared resources in the coordination process - Disadvantages:
A coroutine cannot utilize multi-core resources and is essentially a single thread
The cooperation process needs to cooperate with the process to run on multiple CPU s
There are no mature third-party java libraries at present
It is difficult to debug and debug, which is not conducive to finding problems
The difference between parallelism and concurrency
- Parallel parallelism:
Multiple tasks are processed on multiple CPUs at the same time. When one CPU executes one process, the other CPU can execute another process. The two processes do not preempt CPU resources and can be carried out at the same time - concurrency:
A processor processes tasks at the same time. In fact, it processes multiple tasks alternately at the same time. The program can have two or more threads at the same time. When there are multiple threads operating, if the system has only one CPU, it can not really carry out more than one thread at the same time. It can only divide the CPU running time into several time periods, Then allocate the time period to each thread for execution - Example: the story of project manager A and three programs B, C and D
Parallel: directly find three project managers and assign them to three programmers
Concurrency: A tells B the requirements and B realizes them by himself. During this period, A continues to talk to C and D without waiting for A programmer to complete them. During this period, the project manager is not idle
How to implement multithreading in Java
- Inherit Thread class
Inherit the Thread, rewrite the run method inside, create an instance and execute start
Advantages: code writing is the simplest and direct operation
Disadvantages: there is no return value. After inheriting one class, you cannot inherit other classes, and the expansibility is poor
// Inherit Thread class public class ThreadDemo extends Thread{ @Override public void run() { System.out.println("inherit Thread Implement multithreading, name:"+Thread.currentThread().getName()); } } // Main method (main thread) public class Main { public static void main(String[] args) { ThreadDemo threadDemo = new ThreadDemo(); threadDemo.setName("demo"); // Name the thread threadDemo.start(); // Start threadDemo thread System.out.println("Main thread Name:"+Thread.currentThread().getName()); } }
Operation results:
- Implement Runnable interface
The custom class implements Runnable, implements the run method inside, creates a Thread class, passes the implementation object of the Runnable interface as a parameter to the Thread object, and calls the strat method
Advantages: thread class can implement multiple interfaces and inherit another class
Disadvantages: there is no return value and cannot be started directly. It needs to be started by constructing a Thread instance
// Implement Runnable interface public class RunnableDemo implements Runnable{ @Override public void run() { System.out.println("realization Runnable Interface implements multithreading, name:"+Thread.currentThread().getName()); } } // Main method (main thread) public class Main { public static void main(String[] args) { // Implement Runnable interface to realize multithreading // RunnableDemo runnableDemo = new RunnableDemo(); // Thread thread = new Thread(runnableDemo); // thread.setName("Runnable"); // thread.start(); // System.out.println("main thread Name:" + thread. Currentthread() getName()); // Adopt lambda expression [this method does not need to define ThreadDemo class] Thread thread = new Thread(() -> { System.out.println("realization Runnable Interface implements multithreading, name:"+Thread.currentThread().getName()); }); thread.setName("Runnable"); thread.start(); System.out.println("Main thread Name:"+Thread.currentThread().getName()); } }
Operation results: