Multithreading and high concurrency section 1, summary of classroom knowledge 21.08.01

Posted by Ollie Saunders on Sun, 02 Jan 2022 18:20:28 +0100

Understanding concurrency and parallelism

Concurrency refers to the execution of multiple tasks at the same time for a CPU - in essence, it is divided into time slices. Because the time is very short, it gives the impression that it is executed at the same time;
Parallel is for multiple CPUs to execute multiple tasks at the same time (one CPU executes one), which is real simultaneous execution;

Getting started program analysis (print - HelloWorld)

class Car{
    public void drive(){
        System.out.println("I can run");
    }
}

public class HuiXinThread {
    public static void main(String[] args) {
        Car car=null;
        System.out.println("HelloWorld");//Main thread -- main()
        car.drive();//Exception handling thread---
        new Car().drive();//After running, the new Car() anonymous object will be recycled -- garbage collector
    }
}

Therefore, when we first learned java, there was not only one thread, but actually multithreaded, including the main thread, the thread handling exceptions, and the garbage collector thread;

How do threads preempt CPU resources?

Let's start with a case----
Before we open a program, it is actually the code (static) stored in the computer. After it is opened, the program is started (allocate space and CPU resources for it), and then start different functions in the program - that is, multiple threads are started, and then the CPU serves these threads again. If there are too many threads, the CPU is not enough, This will cause a CPU to execute multiple threads, forming concurrency (using the time slice mechanism). At the same time, priority can be set for threads to improve the probability that the corresponding threads will be executed first (not necessarily later if the priority is low)

Custom thread execution

Thread creation method -
The first - inherits the Thread class

class Car extends  Thread{
    @Override
    public void run(){
        for (int i=0;i<10;i++){
        System.out.println("I can run");}
    }
}

public class HuiXinThread {
    public static void main(String[] args) {
        //Main thread -- main()
        for (int i = 0; i <10 ; i++) {
            System.out.println("HelloWorld"+i);
        }
    new Car().run();

    }
}

This method is not multi-threaded, because it only executes run() as an ordinary method;, Therefore, the running result is to execute the main - main thread first, and then execute run(). At this time, run also belongs to the main thread;

class Car extends  Thread{
    @Override
    public void run(){
        for (int i=0;i<10;i++){
        System.out.println("I can run");}
    }
}

public class HuiXinThread {
    public static void main(String[] args) {
        //Main thread -- main()
        for (int i = 0; i <10 ; i++) {
            System.out.println("HelloWorld"+i);
        }
    new Car().start();

    }
}

start() is the method of the parent class, but the result is the same as above -

The reason is that there are no sub threads involved in the execution of the main thread (for loop). To achieve the concurrency effect, we need to adjust the position of the start() method –

class Car extends  Thread{
    @Override
    public void run(){
        for (int i=0;i<10;i++){
        System.out.println("I can run"+i);}
    }
}

public class HuiXinThread {
    public static void main(String[] args) {
        //Child thread
        new Car().start();
        //Main thread -- main()
        for (int i = 0; i <10 ; i++) {
            System.out.println("HelloWorld"+i);
        }
    }
}

After running, it is found that sometimes concurrency does not necessarily occur when the amount of running is small;

Set and get the thread name for the thread

public class Car extends  Thread{
    @Override
    public void run(){
        for (int i=0;i<10;i++){
            System.out.println(this.getName()+i);}
    }
}

public class HuiXinThread {
    public static void main(String[] args) {
        //Child thread
            Car car= new Car();
            car.setName("a car");
        car.start();
        //Main thread -- main()
        for (int i = 0; i <10 ; i++) {
            System.out.println(Thread.currentThread().getName()+i);
        }
    }
}


You can also set the thread name through the constructor -

public class Car extends  Thread{
    public Car(String name){
        super(name);
    }

    @Override
    public void run(){
        for (int i=0;i<10;i++){
            System.out.println(this.getName()+i);}
    }
}


public class HuiXinThread {
    public static void main(String[] args) {
        //Child thread
            Car car= new Car("puddle jumper");
            //car.setName("car");

        car.start();
        //Main thread -- main()
        for (int i = 0; i <10 ; i++) {
            System.out.println(Thread.currentThread().getName()+i);
        }
    }
}


Set the parent constructor of thread name -

Topics: Java Multithreading Concurrent Programming thread