Thread类源码解读1--如何创建和启动线程

本文源码基于jdk1.8 。

Runnale接口

我们看Thread类的定义知道,它实现了Runable接口

public class Thread implements Runnable {
    ...
}

而Runnable接口的定义如下:

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

它只有一个抽象方法run。同时,该接口还被@FunctionalInterface注解标注,说明它是一个函数式接口(@FunctionalInterface是java 1.8版本之后引入的)。这意味着我们可以使用Lambda表达式来创建Runnable接口的实例,这个我们到后面再举例。

线程创建

在java中,创建一个线程,有且仅有一种方式:

创建一个Thread类实例,并调用它的start方法。

这写在了java语言规范中(参见The Java Language Specification, Java SE 8 Edition, P659,chapter17):

Threads are represented by the Thread class. The only way for a user to create a thread is to create an object of this class; each thread is associated with such an object. A thread will start when the start() method is invoked on the correspon

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值