JAVA multithreaded programming

First, the concept of multi-threaded programming in java

  1. Process : a subprogram executed in the same operating system, including three parts of virtual CPU, code, and data

  2. Multithreading : Multiple parallel subroutines executed in the same operating system. Can increase the cpu usage.

  3. Thread : The flow of subroutines executed in the same process.

  4. Multithreading : Multiple subroutine streams that execute concurrently in the same process. Can increase the cpu usage.

  5. Concurrency : Processes run concurrently. The OS divides time into many time segments (time slices), and distributes them as evenly as possible to the running programs. Microscopically, the process stops and goes, and macroscopically, it is running. This kind of running The phenomenon is called concurrency, but it is not "simultaneous" in the absolute sense.

  6. The difference between process and thread:

    The process has an independent process space, and the data storage space (heap space and stack space) in the process is independent.

    The heap space of the thread is shared, the stack space is independent, and the resources consumed by the thread are smaller than that of the process, which can affect each other.

Two, two methods of thread programming

  1. Inherit the Thread class and override the run() method

  2. Implement the Runnable interface and implement the run() method

Three, the state of the thread

  1. NEW New state: When the program uses the new keyword to create a thread, the thread is in the new state, and the thread has not yet started. When the thread object calls the start() method, the thread starts and enters the Runnable state.

  2. Runnable runnable (ready) state: When the thread is in the Runnable state, it means that the thread is ready and waiting to acquire the CPU

  3. Running (running) state: If the thread acquires the CPU, it enters the Running state and starts to execute the thread body, that is, the content in the run() method. Call the yield() state, which can be the thread entering the Runnable state from the Running state

  4. Block blocking (suspended) state: When the following conditions occur, the thread will enter the blocking state:

    ①The thread calls the sleep() method to voluntarily give up the CPU resources it occupies

    ②The thread calls a blocking IO method (such as the console input method), and the thread is blocked before the method returns

  When the blocking ends, the thread will enter the Runnable state instead of directly entering the Running state.

  5. Dead state of death:

    ① When the execution of the thread's run() method ends, the thread enters the Dead state

    ②The start() method cannot be called on a thread that has died. After the thread dies, it cannot be started again as a thread, and the system will throw an exception.

 

Four, attention

  1、new

http://home.feifeishijie.com/thread-430816-1-1.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324657800&siteId=291194637