Thread、Runnable和Callable,kotlin实现单例

本文深入解析Java中的线程实现,包括Thread类、Runnable接口和Callable接口的使用。通过分析Thread的构造函数和start方法,揭示线程的启动与执行过程。同时探讨了线程的守护线程、优先级等属性,以及如何通过继承Thread或实现Runnable接口创建线程。此外,还提到了线程安全和面试中可能涉及的相关问题。
摘要由CSDN通过智能技术生成

}

2. Thread类

1、Runnable接口源码:

1 public interface Runnable {

2 public void run();

3 }

2、Thread类与Runnable接口的继承关系

1 public class Thread implements Runnable{

2

3 }

Runnable接口仅有一个run()方法,Thread类实现了Runnable接口。

3、构造函数

1 public Thread() {

2 init(null, null, “Thread-” + nextThreadNum(), 0);

3 }

1 public Thread(Runnable target) {

2 init(null, target, “Thread-” + nextThreadNum(), 0);

3 }

1 public Thread(ThreadGroup group, Runnable target) {

2 init(group, target, “Thread-” + nextThreadNum(), 0);

3 }

1 public Thread(String name) {

2 init(null, null, name, 0);

3 }

还有其它的构造方法,此处省略。。。

这里的第三个参数是设置线程的名称,从下面的代码中可以看出,生成名称的规则是:”Thread-”加上创建的线程的个数(第几个)。

继续查看init方法:

复制代码

1 /**

2 * Initializes a Thread.

3 *

4 * @param g the Thread group 5 * @param target the object whose run() method gets called 6 * @param name the name of the new Thread 7 * @param stackSize the desired stack size for the new thread, or 8 * zero to indicate that this parameter is to be ignored.

9 */

//T

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值