Dane Course - Threads

process

It's the whole process of an application running from the beginning to the end.

There may be multiple processes, but the execution of multiple processes is actually a high-frequency switching, not a real "simultaneous" execution.

Switching between multiple processes will show a certain randomness, that is, it will not be exactly the same time to execute process A x first, then process B Y, and then process A x again.

Processes are actually the carriers of threads. There are several threads in the same process.

Every time a process is opened, a thread is opened by default and executed. Such a thread is generally called the main thread.
During the execution of the main thread, developers can create and execute new threads themselves, which are called sub-threads or worker threads.

Whether it's a main thread or a sub-thread, it just starts differently, and there's no other difference.

A single thread executes code from top to bottom, left to right

System.out.println("Main thread starts");

        for(int i=0;i<4;i++){
            System.out.println("Main i="+i);
        }

        System.out.println("Main thread termination");

Output result

Main thread starts
Main i=0
Main i=1
Main i=2
Main i=3
 Main thread termination

Create Thread Chestnut 1

public class CustomThread extends Thread{
    @Override
    public void run() {
        // TODO Auto-generated method stub
        super.run();
        for(int i=0;i<100;i++){
            System.out.println("CustomThread i="+i);
        }
    }
}

MainActivity

public class Test {
    public static void main(String[] args) {
        System.out.println("Main thread starts-----------------");

        CustomThread thread = new CustomThread();
        thread.start();

        for(int i=0;i<100;i++){
            System.out.println("Main i="+i);
        }

        System.out.println("Main thread termination");
    }
}

results of enforcement

The main thread and the sub-thread alternate, and each execution results are different.

Two ways to create and enable threads

Way 1: By inheriting Thread

Custom class, inherited from java.lang.Thread, and override the public void run() method. When you need to start a thread, create a custom class object and call the start() method.

If the code changes to

Create Thread Chestnut 2

System.out.println("Main thread starts-----------------");

        for(int i=0;i<10;i++){
            System.out.println("Main i="+i);
        }

        CustomThread thread = new CustomThread();
        thread.start();


        System.out.println("Main thread termination");

Put the for loop of the main thread at the beginning, and the result is

Main thread starts-----------------
Main i=0
Main i=1
Main i=2
Main i=3
Main i=4
Main i=5
Main i=6
Main i=7
Main i=8
Main i=9
//Main thread termination
CustomThread i=0
CustomThread i=1
CustomThread i=2
CustomThread i=3
CustomThread i=4
CustomThread i=5
CustomThread i=6
CustomThread i=7
CustomThread i=8
CustomThread i=9

The main thread will be executed first, and then the sub-thread will be executed. In fact, it is very easy to understand. Draw a picture to understand.

Picture of Chestnut 1

In chestnut 1, when the main thread is executed to ct.start(), a thread is opened, and then the sub-thread and the main thread are executed alternately.

Picture of Chestnut 2

In chestnut 2, the execution of ct.start(); when the sub-thread is opened, the main thread's for loop has already been executed, only the last sentence, System.out.println(); may be inserted in the sub-thread, so the execution effect just mentioned will appear.

Mode 2: Implementing Runnable Interface

public class CustomRunner implements Runnable{

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for(int i=0;i<10;i++){
            System.out.println("CustomRunner i="+i);
        }
    }
}

You can no longer call the start() method when you enable threads

Because CustomRunner implements the Runable interface, there is no such method in this interface. It directly inherits java.lang.Object, and of course there is no such method in Object.

The object of CustomThread can call the start method because it inherits Thread, and there is a start method in Thread.

The right way is

CustomRunner runner = new CustomRunner();
Thread r = new Thread(runner);
r.start();

Custom class, implement java.lang.Runnable, and rewrite the public void run() method. When a thread needs to be started, create the object of the custom class, and use the object as the parameter of Thread's construction method, create the Thread object, and then call the start() method of the Thread object.

Keywords: Java

Added by wad on Fri, 17 May 2019 16:19:34 +0300