Java multithreading learning (1) java thread Runnable, Thread, Callable, FutureTask

The way the thread is created

1.Thread object

1.1 Inherit the Thread object

@Slf4j
public class MyThread extends Thread{
    
    
    @Override
    public void run() {
    
    
        // 线程要执行的内容
        log.debug("继承Thread方式");
    }
}

public class Test1 {
    
    
    public static void main(String[] args) {
    
    
        new MyThread().start(); // 启动线程
    }
}

1.2 Use the Thread object directly

public class Test1 {
    
    
    public static void main(String[] args) {
    
    
        Thread t = new Thread(){
    
    
            @Override
            public void run() {
    
    
                log.debug("线程执行");
            }
        };
        t.start();
    }
}

2.Runnable interface

public class MyThread implements Runnable{
    
    
    @Override
    public void run() {
    
    
        // 该线程执行内容
        System.out.println("线程执行");
    }
}

public static void main(String[] args) {
    
    
        MyThread myThread = new MyThread();
        Thread t1 = new Thread(myThread, "t1");
        t1.start();

        // jdk8 Lambda
        Thread t2 = new Thread(()->{
    
     System.out.println("t2执行");}, "t1");
        t2.start();

        Runnable runnable = ()->{
    
     System.out.println("t3执行"); };
        Thread t3 = new Thread(runnable, "t3");
        t3.start();
    }

3. The relationship between Runnable and Thread

Runnable needs to be passed into Thread for execution

      Runnable runnable = ()->{
    
     System.out.println("t3执行"); };
      Thread t3 = new Thread(runnable, "t3");
      t3.start();

Click on the Thread source code and find that Runnable will be passed to an init method

https://img-blog.csdnimg.cn/20201027204059703.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQzNTM5OTYy,size_16,color_FFFFFF,t_70#pic_center
It is found that init is passed to another init.
Insert picture description here
The init method is found to have passed a variable of itself

Insert picture description here
It is found that the run() method in the Thread class finally calls the run method of this Runnable

Insert picture description here

The run of Runnable is ultimately the run of Thread. If the run method of Thread is not rerun, the run method of Thread will first determine whether there is Runnable passed in, and if so, execute the run method of Runnable.

3.Callable和FutureTask

FutureTask itself implements RunnableFuture

public class FutureTask<V> implements RunnableFuture<V>

RunnableFuture inherits Runnable and Future

public interface RunnableFuture<V> extends Runnable, Future<V>

That is to say, FutureTask can also be regarded as a Runnable but it is stronger than Runnable
because it inherits the method in Future, which gives it the ability to obtain thread running results.
Insert picture description here
Callable is just an interface. Its call and Runnable run methods are similar in nature, and they are all threads. Run the code in it, but call has a generic return value and can also throw exceptions. The run method does not have exceptions and has to be digested internally.

Insert picture description here

But when we run the code, we still use the start method. It still runs the run method, but we found that we did not rewrite the run method but a call method.

Insert picture description here

Click on the FutureTask class and find that it has overridden the run method inside. When we start the thread at start, the run method will call the call method of the incoming Callable, and handle the exception here. The value is returned to the set method of FutureTask for the call of methods such as get

Insert picture description here

The value is returned to the variable outcome

Insert picture description here
When the get method is called, the report method
Insert picture description here
is called. The return of the method is the value of the run method calling call

Insert picture description here
Note
Insert picture description here
that the get method has a return value, which means that it will block until the thread returns asynchronously.

Guess you like

Origin blog.csdn.net/qq_43539962/article/details/109300838