Java Foundation 24 Multithreading (Thread)

1.1. Process

    The executing program is called a process. The process is responsible for the division of the memory space

Question 1 : Windows computers are called multi-tasking operating systems, so how does Windows run multiple applications at the same time?
    From a macro perspective: Windows does run multiple programs at the same time.
    From a microscopic point of view: the cpu is doing a fast switching action, the speed is too fast, so you can't feel the switching.

1.2, thread

    A thread is responsible for the execution of code in a process, an execution path in the process

1.3. Multithreading

    In a process, there are multiple threads executing different tasks at the same time

Question 2 : The thread is responsible for the execution of the code. We have not learned about the thread before, why the code can be executed? 
   Any java program will create a main thread to execute all the code in the main method when the jvm is running.

1.4, the benefits of multi-threading

    1. Solve the problem that one process can perform multiple tasks.
    2. Improve the utilization of resources

1.5. Disadvantages of multithreading

    1. Increases the burden on the CPU
    2. Reduces the probability of thread execution in a process
    3. Causes thread safety issues
    4. Deadlock occurs

1.6, the way to create multithreading

Method 1:
    1. Customize a class to inherit the Thread class
    2. Rewrite the run method in the Thread class, and write the code of the custom thread in the run method
    3. Create a subclass object of Thread, and call the start method to start the thread.

  Note: Once a thread is started, the thread will execute the code in the run method. The run method must not be called directly. If it is called directly, it is equivalent to an ordinary method, and a new thread will not be opened.

Question 3 : What is the purpose of rewriting the run method? 
   Each thread has its own task code. The task code of the jvm to create the main thread is all the code in the main method. The task code of the custom thread needs to be written in the run method. , the custom thread is responsible for the code in the run method

1.7. Examples

1  package com.zn.thread;
 2  
3  /* *
 4  * @author DSHORE / 2018-5-3
 5  *
 6   */ 
7  /* Ways to create multiple threads
 8  * Way 1:
 9  * 1. Customize a class Inherit the Thread class
 10  * 2. Rewrite the run method in the Thread class, and write the code of the custom thread in the run method
 11  * 3. Create a subclass object of Thread, and call the start method to start the thread.
 12  *
 13  * Note : Once a thread is started, the thread will execute the code in the run method. Do not call the run method directly. If you call it directly, it is equivalent to an ordinary method, and a new thread will not be opened.
 14  * 
 15  * * / 
16  public  class Demo1 extends Thread { //1. Customize a class to inherit the Thread class 
17      @Override
 18      public  void run() { // 2. Rewrite the run method in the Thread class, and write the code of the custom thread in the run method 
19          for ( int i= 0 ;i< 100 ;i++ ){
 20 System.out              .println ( " custom thread " + i);
 21          }
 22      }
 23      public  static  void main(String[] args) {
 24          Demo1 d= new Demo1(); / / 3. Create a subclass object of Thread, and call the start method to start the thread.
25         d.start();
26         
27         for(int i=0;i<100;i++){
28             System.out.println("主线程"+i);
29         }
30     }
31 }

Operation result graph: Note: The result of each operation is different, the thread problem

 

 

 

 

 

Original Author: DSHORE

From: http://www.cnblogs.com/dshore123/

Welcome to reprint, reprint must indicate the source. ( If this article is useful to you, you can click Recommend , thank you! )

Guess you like

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