Runnable和Thread

2018-07-22  本文已影响0人  hei禹

Runnable接口

@FunctionalInterface
public interface Runnable {
    public abstract void run();
}

衍生知识点:@FunctionalInterface 函数式接口

Thread类介绍

public class Thread implements Runnable {
    
    ...
    
    @Override
    public void run() {
        if (target != null) {
            target.run();
        }
    }
    
    ...
}

target是一个Runnable对象,如果不为null,则调用其run方法。

target是在init方法中初始化的,所有构造函数都会调用init。因此下面看下Thread类有哪些构造函数:

init函数:

private void init(ThreadGroup g, Runnable target, String name,
                  long stackSize, AccessControlContext acc,
                  boolean inheritThreadLocals); 

Thread类的构造函数:

Thread()
Thread(Runnable target)

Thread(Runnable target, String name)
Thread(String name)
Thread(ThreadGroup group, Runnable target)
Thread(ThreadGroup group, Runnable target, String name)
Thread(ThreadGroup group, Runnable target, String name, long stackSize)
Thread(ThreadGroup group, String name)

基本方法:

void start();

void run();

如果构造时未传递Runnable对象,则调用run方法直接返回,否则执行Runnable对象的run方法

run方法一般不需要显示调用(JVM会帮你调用),只需要调用start方法即可;如果直接调用run方法,会在原线程中执行,并不会新起线程

使用方法

  1. 继承Thread类,重写run方法
public class MyThread extends Thread {
    @Override
    public void run() {
        System.out.println("my thread extends java.lang.Thread");
    }
}

new MyThread().start();

2.实现Runnable接口,重写run方法

public class MyThread2 implements Runnable{
    public void run() {
        System.out.println("my thread implements java.lang.Runnable");
    }
}

new Thread(new MyThread2()).start();
  1. 匿名类的写法(实质是1,2)
new Thread(new Runnable() {
    public void run() {
        System.out.println("Runnable run");
    }
}).start();

new Thread() {
    @Override
    public void run() {
        System.out.println("Thread run");
    }
}.start();

4、Java8 lambda写法(实质是2)

new Thread(() -> System.out.println("lambda")).start();

2种创建线程方法的区别、优劣

Thread类:

Runnable接口:

相关内容

上一篇下一篇

猜你喜欢

热点阅读