创建线程3种方式

1.继承Thread类:

(1)创建一个类继承Thread类,重写run()方法,将所要完成的任务代码写进run()方法中;

(2)创建Thread类的子类的对象;

(3)调用该对象的start()方法,该start()方法表示先开启线程,然后调用run()方法;

public class Threads {
public static void main(String[] args) {

        Tssdf tssdf = new Tssdf();
        ts
        tssdf.start();//新建
}
}
 //创建线程
     class Tssdf extends Thread{
        @Override
        public void run() {
            System.out.println("hello,Thread");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

2.实现Runnable接口

Runnable接口实现 线程要执行的任务,只有一个抽象方法run() ;

(1)创建一个类并实现Runnable接口

(2)重写run()方法,将所要完成的任务代码写进run()方法中

(3)创建实现Runnable接口的类的对象,将该对象当做Thread类的构造方法中的参数传进去

(4)使用Thread类的构造方法创建一个对象,并调用start()方法即可运行该线程

具体实现

public class Threads {
    public static void main(String[] args) {

        Tsets tsets = new Tsets();
        Thread thread = new Thread(tsets);
        thread.start();

    }
}
 //创建线程
class Tsets implements Runnable {
        @Override
        public void run() {
            System.out.println("hello,Run");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }



优点:

1.类只能单继承,但实现了Runnable接口,还可以继承其他的类,实现其他的接口

2.实现Runnable接口的方式,把设置线程任务和开启新线程进行了分离

为什么将Runnable子类作为Thread构造函数参数:

因为,自定义的run方法所属的对象是Runnable接口的子类对象。所以要让线程去指定指定对象的run方法。就必须明确该run方法所属对象。

Thread类的run也是继承的Runnable

ps:lambda简化方式

具体实现

@FunctionalInterface
//“函数式接口”是指仅仅只包含一个抽象方法的接口。
// Runnable 接口只有run()方法
//可以用lambda表达式简化
  //lambda表达式
        Runnable runnable= ()->{
            System.out.println("lambda");
        };
        Thread t=new Thread(runnable,"l");
        t.start();

3.FutureTask+Callable实现带返回结果的线程

(Runnable接口没有返回结果)
源码角度分析:

public class FutureTask<V> implements RunnableFuture<V> {
 public FutureTask(Callable<V> callable) {
        if (callable == null)
            throw new NullPointerException();
        this.callable = callable;
        this.state = NEW;       // ensure visibility of callable
    }
}

可以发现也是继承Runnable接口,多了一个Future接口

public interface RunnableFuture<V> extends Runnable,Future<V> {
    /**
     * Sets this Future to the result of its computation
     * unless it has been cancelled.
     */
    void run();
}

Future可以有返回结果

public interface Future<V> {
    //获得返回结果
    V get() throws InterruptedException, ExecutionException;
 ...}

Callable可以处理异常

@FunctionalInterface
public interface Callable<V> {
    V call() throws Exception;
}

具体实现

FutureTask<Integer> task=new FutureTask<Integer>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                System.out.println("future");
                Thread.sleep(1000);
                return 2020;
            }
        });
        Thread thread1=new Thread(task);
        thread1.start();
        Integer integer = task.get();//获得返回值,会等待
        System.out.println(integer);


关于Callable:Callable接口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值