First acquaintance with java multithreading technology

1. Overview of processes and threads

A process can be directly understood as a running program. A process can execute multiple threads, and threads depend on the process.
注意:A multi-process computer does not mean that the CPU can perform multiple tasks at the same time. In fact, multi-process computers make us feel that the CPU is performing multiple tasks at the same time through continuous and efficient switching of programs .
Thread into 单线程and 多线程. Multithreading means that the program has multiple paths to perform the same task. The essence of multithreading is actually to have more opportunities to obtain the execution rights of the CPU . It is precisely because of this feature that multithreading will have thread safety issues. The existence of multithreading is not to increase the execution speed of the program, but to increase the utilization rate of the application.

Two. Multi-threaded implementation

1. Through the Thread class

The specific operations are: inherit the Thread class, rewrite the run method, create an instance of the Thread subclass (that is, create a thread object), and call the start method to start the thread. Test code:

package package03_thread;

public class ThreadDemo01 extends Thread {
    
    
    //车票数量
    private int tickets = 10;

    @Override
    //重写run方法
    public void run() {
    
    
        while (tickets > 0) {
    
    
            System.out.println(Thread.currentThread().getName() + "卖出第【" + tickets-- + "】张火车票");
        }
    }

    public static void main(String[] args) {
    
    
        //调用start方法启动线程
        new ThreadDemo01().start();
        new ThreadDemo01().start();
        new ThreadDemo01().start();
    }
}

Run screenshot:

Insert picture description here
Note that the Thread method does not share data between threads and can only inherit the Thread class.

2.Runable interface implementation

The specific implementation is: rewrite the run method, create an instance of the Runable implementation class, and use this instance to pass in the Thread object, create the Thread object, and call the start method to start the thread.
Test code:

package package03_thread;

public class RunableDemo01 implements Runnable{
    
    
    private int tickets = 10;
    @Override
    public void run() {
    
    
        while(tickets > 0) {
    
    
            System.out.println(Thread.currentThread().getName()+"卖出第【"+tickets--+"】张火车票");
        }
    }

    public static void main(String[] args) {
    
    
        Runnable runnable = new RunableDemo01();
        new Thread(runnable).start();
        new Thread(runnable).start();
        new Thread(runnable).start();
    }
}

Run screenshot:
Insert picture description here
This method realizes data sharing, and at the same time, the same runable object is handed over to multiple threads for processing, effectively separating code and data. In general, this method is more commonly used.

3. Thread pool implementation

Test code:

package package03_thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class PoolDemo01 implements Runnable{
    
    
    @Override
    public void run() {
    
    
        System.out.println(Thread.currentThread());
    }
    private static int Pool_num = 10;

    public static void main(String[] args) {
    
    
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        for (int i = 0; i < Pool_num; i++) {
    
    
            PoolDemo01 poolDemo01 = new PoolDemo01();
            executorService.execute(poolDemo01);
        }
        executorService.shutdown();
    }
}

Run screenshot:
Insert picture description here

Guess you like

Origin blog.csdn.net/m0_46550452/article/details/107215361