High Concurrent Programming Fundamentals

    The basis of concurrent programming, should be inseparable from the multi-threaded, ask this briefly explain the basis of multi-threaded.

First, the multi-threaded implementation

    As we know, there are four ways to create a thread, namely:

   1. Thread class inheritance override the run method

   2. Implement Runnable interface, override the run method

   3. Anonymous inner classes way

   4. Thread Pool

   In general, are to operate around the Thread class and Runnable interface, work should use the most is the thread pool (after the presentation).

And two related threads of the concept of threads and processes

        Process is a computer program run on a set of data on the activities of the system is the basic unit of resource allocation and scheduling. A thread is the smallest unit of program execution. For chestnut: high-speed rail station is a process, high-speed rail station responsible person (system) will have the entire high-speed rail station resource allocation and scheduling, each thread is a railway.

Third, the classification of thread

        Users are divided into threads and daemon threads, user threads: user-defined thread is created, the main thread to stop, will not stop the user thread; daemon thread: daemon thread when the process does not exist or stop the main thread, the thread daemon will be stopped.

        Daemon thread usage is also very simple to use setDaemon (true) method to set a daemon thread, you need () set before the start, otherwise it will set up a daemon thread failure, but as a user threads.

Fourth, the threads of the life cycle

       New state, ready state, running state, blocking state and the state of death.

       Ready, running, blocking three states can switch back and forth, such as: ready state to run state, running transfer blocking, blocking can then back running. It does not come to death, it has survived.

Five, java memory model

       When referred to JMM, decided a thread writes to shared variables, it can be seen on another thread. A shared resource is stored in main memory, each thread will save a copy of the shared resource, when a copy of a thread to operate, the resource changes flushed to main memory, and then notify other threads refresh local memory copy, thus ensuring visibility. From an abstract point of view, JMM defines an abstract relationship between the thread and the main memory: the main memory (main memory), each thread has a private local memory (local memory) shared among threads variable storage , local memory stored a copy of the thread to read / write shared variables. JMM local memory is an abstract concept, and are not real. It covers the cache, write buffer, registers and other hardware and compiler optimizations.

     Three characteristics.

     1, atomicity

         When a non-interruptible operation. Even when multiple threads of execution together, a single operating a start, it will not be disturbed by other threads.

     2, visibility

When a thread modifies the value of a variable of a shared, other threads can immediately know whether this modification. Usually in the use of global shared variables to ensure the visibility of the volatile keyword, timely resources brush into the main memory, and will prohibit reordering.

    3, orderliness

For a thread, the execution of the code are in accordance with the future in order from first to perform. However, when the concurrent execution of the program might appear out of order. Causes scrambled because the program is executed, an instruction may be rearranged.

The so-called reordering is to be no relationship between the dependent instruction reordering to improve efficiency. Similarly pipeline works, there are two such instructions, instruction A and instruction B, instruction A = a + b, instruction B = c + d, of these two instructions have no dependencies, the compiler and processor may be operation do reordered. If the instruction A = a + b, instruction B = A + c, then the two instructions have dependencies will not be reordered.

 

Published 18 original articles · won praise 2 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_32285039/article/details/103277523