20165118 Eighth week study summary

Chapter 12: JAVA Multithreading Mechanism

1. Processes and threads

  • A process is a dynamic execution of a program.
    Java's multithreading mechanism. Java has built-in support for multithreading. Our computer can only execute one of the threads at any given time, and the Java Virtual Machine simply switches quickly from one thread to another.
  • The Java virtual machine quickly switches control from one thread to another. These threads will be executed in turn, giving each thread a chance to use CPU resources.

2. Thread class and thread creation

  • When writing a subclass of the Thread class, you need to override the run() method of the parent class. The purpose is to specify the specific operation of the thread, otherwise the thread will do nothing, because there is no operation statement in the run() method of the parent class. .
  • Another way to create a thread is to use the Thread class to create a thread object directly. The commonly used constructor to create a thread with Thread is:

    Thread(Runnable target)

3. Common methods of threads

start();

run();

sleep (int millsecond) ;

isAlive();

currentThread();

interrupt();

4. Thread synchronization

Synchronization mechanism: When a thread uses a synchronized method, other methods can only wait for the thread to finish using the method before using it.

5. Coordinate the synchronization process

(1) The wait() method can interrupt the execution of the method, make the thread wait, temporarily give up the right to use the CPU, and allow other threads to use this synchronization method

(2) The notifyAll() method notifies all threads that are waiting due to the use of this synchronization method to end waiting. The thread that has been interrupted will continue to execute this synchronization method from the point where it was interrupted just now, and follow the principle of "interrupt first, continue first"

(3) The notify() method just notifies one of the waiting threads to end waiting

6. Timer thread

7. Daemon threads

Guess you like

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