当前位置:网站首页>第五章 我们可以直接调用run()方法来启动一个新线程吗
第五章 我们可以直接调用run()方法来启动一个新线程吗
2022-07-17 05:14:00 【wwwarewow】
不,你不能直接调用run方法来启动一个线程。您需要调用 start 方法来创建一个新线程。 如果你直接调用run方法,它不会创建一个新线程,它会和main在同一个栈中。 让我们在示例的帮助下理解:
package org.arpit.java2blog;
class CustomThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread is running :"+i);
}
}
}
public class StartThreadAgainMain {
public static void main(String[] args) {
CustomThread ct1 = new CustomThread();
CustomThread ct2 = new CustomThread();
ct1.run();
ct2.run();
}
}当您运行上面的程序时,您将得到以下输出:
Thread is running :0 Thread is running :1 Thread is running :2 Thread is running :3 Thread is running :4 Thread is running :0 Thread is running :1 Thread is running :2 Thread is running :3 Thread is running :4
正如您所看到的,当我们直接调用 run 方法时,它并没有创建新线程。
如果您在上面的示例中使用 start 而不是 run:
package org.arpit.java2blog;
class CustomThread extends Thread {
public void run() {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread is running :"+i);
}
}
}
public class StartThreadAgainMain {
public static void main(String[] args) {
CustomThread ct1 = new CustomThread();
CustomThread ct2 = new CustomThread();
ct1.start();
ct2.start();
}
}当您运行上面的程序时,您将得到以下输出:
Thread is running :0 Thread is running :0 Thread is running :1 Thread is running :1 Thread is running :2 Thread is running :2 Thread is running :3 Thread is running :3 Thread is running :4 Thread is running :4
不能直接调用run方法创建线程,需要调用start方法创建新线程。
边栏推荐
- Wireless charging chip IC
- 有依赖的背包,狭义(二进制枚举),广义(树形dp)
- 2022 robocom world robot developer competition - undergraduate group (provincial competition)
- filezilla传输虚拟机速度慢解决方法
- Decorate Apple Tree
- Simple chrome script automatically skips the charging acknowledgment page after the video playback of station B ends
- Decorate Apple Tree
- MCU single chip OTP
- XOR-gun (位运算,思维,区间暴力)
- 设置索引库结构,给用户添加可自动补全的suggestion,并将一些字段变成集合放到suggestion里面去
猜你喜欢

High voltage module isolation module hra2460d-2w

Go language introduction and application scenario analysis

5-17陕西科技大学的隐藏学生服务

【简单快速】启动后桌面正常下方任务栏无反应/鼠标一直转圈

Hm9922 switching buck LED constant current driver IC

DSL实现自动补全查询

Complete scheme diagram of lth7 five pin chip fs4054 charging circuit principle

VSCode 即时英文翻译插件 【翻译(英汉词典)】

Introduction to basic knowledge of Minio

软考初、中、高级考试全体验
随机推荐
Acwing第57场周赛(AK)
It4058a single lithium ion battery charging management
Solve cannot read properties of null (reading 'pickalgorithm')
Fs5383a lithium battery 3.7V input power supply solar lawn lamp drive IC
自动补全 & (自定义)拼音分词器 & 搜索时注意事项
【力扣】设计循环队列
计算几何(2)
2022 robocom world robot developer competition - undergraduate group (provincial competition)
【简单快速】启动后桌面正常下方任务栏无反应/鼠标一直转圈
4-20mA to 0-5khz, 5V pulse converter
Darwin 反射总结
2022/07/14 learning notes (day07) array
2022 RoboCom 世界机器人开发者大赛-本科组(省赛)
DSL实现Bucket聚合
Baby Ehab Partitions Again(dp,构造,位运算)
BusyBox 1.21.1 有udpsvd功能 可以编译成功 不干涉本机busybox方法
2021-09-15
Loadng class `com.mysql.jdbc.Driver‘. This is deprecated. The new driver class is `com.mysql.cj.jdb
单表查询、添加、更新与删除数据
2021-09-15