The difference between a process and a thread

(1) A thread only belongs to a process, a process can have more than one thread

(2) The processes are independent from each other, and threads share the memory space and resources of the process

(3) A process is a unit that allocates resources and has an independent address space

(4) Thread is the basic unit of processor scheduling

Several modes of interprocess communication:

(1) Pipelines. Half – duplex communication, often used in related parent – child processes

(2) Named pipes. Used of processes that are not related

(3) Message queue. Stored in the kernel to overcome the drawbacks of signals and pipes

(4) Shared memory. A process is created and shared by multiple processes. It is usually used with semaphores for communication and synchronization between processes

(5) Signals. Notifies an event that has occurred

(6) Semaphore, is a counter, used to control the access of multiple processes to shared resources, is a locking mechanism, to solve the mutual exclusion and synchronization between processes

Why multithreading

The use of multithreading, some large tasks can be decomposed into a number of small tasks to execute, a number of small tasks between each other, at the same time, so that the full use of CPU resources.

There are two ways to implement multithreading:

(1) Inherit Thread class and rewrite run method;

(2) Implement Runable interface, implement run method;

Thread status:

New, ready, Running, blocked, dead

Thread pools are used for:

(1) Reduce resource consumption and reuse

(2) To accelerate the response speed, do not need to repeat the creation

(3) Easy to manage

Thread pools in Spring

Thread pools in Spring are implemented by the ThreadPoolTaskExecutor class

Java code initialization:

private void test2(){ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); Executor. SetCorePoolSize (10); Executor. SetMaxPoolSize (15); Executor. SetKeepAliveSeconds (1); Executor. SetQueueCapacity (5); Executor. SetRejectedExecutionHandler (new ThreadPoolExecutor. CallerRunsPolicy ()); Executor, the initialize (); executor.execute(newRunnable() {@ Override public voidrun() {// execute code}}); }Copy the code

Thread communication

1. Communication mode

(1) Wait notification mechanism: Two threads communicate by calling wait() and notify() on the same object

Example: Two threads alternately print odd and even numbers to gain insight into thread communication

(2) Join () method: actually call wait notification mechanism

(3) Volatile shared memory: Java uses shared memory (or pipes)

(4) Thread response interrupt

(5) awaitTermination() of the thread pool

(6) Pipeline communication

The volatile keyword

Typical case: I ++ and ++ I are not atomic operations

Effect: Ensures memory visibility and prevents JVM instruction rearrangements from optimizing volatile

Explanation: The speed of the CPU is often difficult to match the speed of the memory, so a cache is inserted between the CPU and memory (each thread has its own cache), and the memory is fetched when the CPU does not hit the cache. In concurrent programming, the cache is not synchronized to memory.

With volatile variables will force any operation thread to this variable will immediately flushed to main memory, and forced to cache the stack to empty this variable, must get data from main memory, and guarantee the consistency (note that after modification and not let the thread from the main memory, still copies variables into a buffer). So it is guaranteed that the fetched values are the latest, but note that volatile does not guarantee atomicity or thread-safety