Java multithreading and thread creation

Multithreading

1. Thread

1.1 Process

A process is a program that is running (in memory)

  • It is an independent unit for the system to perform resource allocation and scheduling
  • Each process has its own storage space and system resources

1.2 thread

A thread is a single flow of control in a process, a single path of execution

  • Single-threaded: one process contains one sequential flow of control (one path of execution)
  • Multithreading: Multiple sequential control flows (multiple paths of execution) in a single process
  • Multithreading can improve the processing efficiency of the program

2. Multithreading

2.1 Multithreading concept

Multi-threading refers to the technology to realize the concurrent execution of multiple threads from software or hardware. Computers with multi-threading capabilities can execute more than one thread at the same time due to hardware support, which improves the overall processing performance. Systems with this capability include parallel multiprocessors, multicore processors, and chip-level multiprocessing or simultaneous multithreading processors. In a program, these independently running program fragments are called "threads", and the concept of programming using them is called "multithreading".

2.2 Advantages and disadvantages of multithreading

advantage:

(1) The user interface can keep the CPU active while doing other work, which can make the program run faster.

(2) Annoyance that takes up a lot of processing time can periodically give up processor time to other tasks, which can improve CPU utilization

shortcoming:

(1) Waiting to use shared resources causes the running speed of the program to slow down. These shared resources are mainly exclusive resources, such as printers, etc.

(2) Thread management requires additional CPU overhead, and the use of threads will bring additional context burden to the system.

(3) Thread deadlock, that is, a deadlock may occur during the process of locking shared resources to achieve synchronization.

2.3 Thread creation method

The thread class Thread is encapsulated in JavaJDK, and a thread can be created only by creating this type of object. Thread is the execution thread in the program. The Java virtual machine allows applications to run multiple threads of execution concurrently.

Two ways to create threads

1. Define a class to declare this type of description as a subclass of Thread (inherited from the Thread type), and rewrite the run method in Thread. Starts an instance of the current class.

@Override
    public void run(){
    
    
        //在这里书写这个线程需要执行的业务逻辑
        for(int i = 0; i <= 10;i++){
    
    
            System.out.println("输出结果"+i);
        }
    }
}

To start this thread, an "entry" is required, and the Java program has only one entry function—main method

public class Main {
    
    
    public static void main(String[] args) {
    
    
        //main方法也相当于自定义线程的run方法,且每个主函数都成为主线程。一个项目只有一个主线程
        //可以在这里启动自己创建的线程类
        //创建定义的MyThread的对象
        MyThread mt = new MyThread();
        //调用start方法
        mt.start();

    }
}

result:

insert image description here

2. Define a class that implements the Runnable interface and override the run method. You need to pass the current object of this class through the instantiation of the Thread class, and finally start the instance of the created Thread class.

Create a class that implements the Runnable interface and override the

/**
 * 这个类是runnable接口的实现类,不是线程类
 */

public class MyRunnable implements Runnable{
    
    
    
    @Override
    public void run(){
    
    
        //用来书写当前启动的线程中需要执行的逻辑
        for (int i = 0; i < 10; i++){
    
    
            System.out.println("我是runnale实现的线程"+i);
        }
    }
    
}

Create a thread object in the main class, and pass the runnable implementation class object to the thread object to get a thread object

public class RunnableMain {
    
    
    public static void main(String[] args) {
    
    
        //1.需要创建Runnable实现类的对象
       MyRunnable  mr=  new MyRunnable();

       //2.创建一个线程对象,并将Runnable对象传递到Thread对象
        Thread r = new Thread(mr);

        //3.使用线程类的start方法启动线程
        r.start();
    }
}

result:

insert image description here

Guess you like

Origin blog.csdn.net/weixin_54291763/article/details/127735262