1. Concept and thread creation

concept

Program: an ordered collection of instructions and data. It has no meaning of running. It is a static concept

Process: an execution process of a program. It is a dynamic concept with its own life cycle. It is the basic unit of system resource allocation

Thread: it is the basic unit of CPU scheduling and execution

  • When a program runs, there is at least one process, and a process has at least one thread
  • When the program runs, even if it does not create a thread, there will be two threads in java, namely, the main thread and the daemon thread
  • main() is called the main thread and is the entry of system execution
  • When there are multiple threads, the operation of the thread is scheduled by the scheduler. The scheduler is closely related to the operating system, and the sequence cannot be interfered by human beings. The thread has priority, but the final call depends on the CPU
  • The system will allocate resources to the process, and multiple threads in the process will share this resource. Therefore, multithreading will rob resources, and concurrency control needs to be added
  • Threads bring additional overhead, such as CPU scheduling and concurrency control overhead
  • Many multithreads are simulated, that is, in the case of one CPU, the CPU can only execute one thread at the same time point, but the switching speed is very fast, so it will be considered that multiple threads execute at the same time. True multithreading means having multiple CPUs. That is, multi-core.

Common method calls and multithreading

Three ways to create threads

Thread class

Thread implements the Runnable interface
Steps: 1. The custom Thread class inherits the Thread class; 2. Rewrite the run() method and write Thread execution code; 3. Create a custom Thread object and call the start() method to start the Thread

//Thread creation method 1: inherit the thread class, rewrite the run method, and call start to start the thread
public class TestThread01 extends Thread{
    @Override
    public void run() {
        //run method thread body
        for (int i = 0; i < 2000; i++) {
            System.out.println("I'm looking at the code---"+i);
        }
    }
    public static void main(String[] args) {
        //Main main thread
        //Create a thread object
        TestThread01 testThread01 = new TestThread01();
        //Thread start
        testThread01.start();

        for (int i = 0; i < 2000; i++) {
            System.out.println("I'm studying---"+i);
        }
    }
}

Runnable interface

This method is recommended. 1. Because of the limitations of java single inheritance, it is best to implement the interface rather than inheritance; 2. The idea of static agent can be one resource and multiple agents (described in detail in the following chapters)
Steps: 1. The user-defined class implements the Runnable interface; 2. Implement the run() method and write the Thread execution body; 3. Create an object of a custom class and use it as a parameter of the Thread object; 4,start()

The following example has opened up three threads. Only one custom class object and three Thread objects are required. You can also name the Thread

public class TestThread01 implements Runnable{
    @Override
    public void run() {
        //run method thread body
        for (int i = 0; i < 2000; i++) {
            System.out.println("I'm looking at the code---"+i);
        }
    }

    public static void main(String[] args) {
        //Main main thread
        //Start three threads
        TestThread01 test = new TestThread01();
        new Thread(test,"A").start();
        new Thread(test,"B").start();
        new Thread(test,"C").start();

        for (int i = 0; i < 2000; i++) {
            System.out.println("I'm studying---"+i);
        }
    }
}

Callable interface

Test: multi thread downloading pictures

You need to import a commons-io-2.6 Jar package, using FileUtils The copyurltofile (new URL (URL), new file (name)) method downloads resources from the network and passes in the URL and storage address

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;
//Inherit Thread
public class TestThread02 extends Thread{
    private String url;
    private String name;

    @Override
    public void run() {
        try {
            new WebDowner().down(url,name);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(name+"Download exception");
        }
    }

    public TestThread02(String url, String name) {
        this.url = url;
        this.name = name;
    }

    public static void main(String[] args) {
        TestThread02 t1 = new TestThread02("https://img0.baidu.com/it/u=2151136234,3513236673&fm=26&fmt=auto&gp=0.jpg", "C:\\Users\\Bi\\Desktop\\1.jpg");
        TestThread02 t2 = new TestThread02("https://c-ssl.duitang.com/uploads/item/201503/21/20150321160159_vVxSC.thumb.1000_0.jpeg","2.jpg");
        TestThread02 t3 = new TestThread02("https://c-ssl.duitang.com/uploads/item/201503/12/20150312231447_THNsw.thumb.1000_0.jpeg","3.jpg");

        t1.start();
        t2.start();
        t3.start();
    }
}

class WebDowner{
    //Download method
    public void down(String url, String name) throws Exception {
        FileUtils.copyURLToFile(new URL(url), new File(name));
        System.out.println(name+"Download complete");
    }
}

Keywords: Multithreading

Added by paqman on Thu, 23 Dec 2021 05:52:12 +0200