当前位置:网站首页>创建线程的3种方式
创建线程的3种方式
2022-07-28 07:15:00 【TPH-BETTER】
转自https://blog.csdn.net/weixin_49920327/article/details/123561378
1. 继承Thread类
**1)创建一个类,继承 Thread并重写 run 方法 **
2)创建对象 执行 start 方法
//创建线程方式一:继承Thread类,重写run()方法,调用start()开启线程
public class TestThread1 extends Thread{
@Override
public void run() {
//run()方法线程体
for (int i = 0; i < 20; i++) {
System.out.println("我在看代码——————" +i);
}
}
public static void main(String[] args) {
//main()线程,主线程
//创建一个对象
TestThread1 testThread1 = new TestThread1();
//调用start()方法
testThread1.start();
for (int i = 0; i < 20; i++) {
System.out.println("我在学习多线程" + i);
}
}
}
2. 实现Runnable接口
1)创建一个类 实现Runnable接口并实现 run 方法
2)创建一个 该类 对象
3)创建一个 Thread 对象,构造方法入参为 该类 对象
4)Thread 对象 执行start方法
//创建线程方式2:实现runnable接口,重写run方法,执行线程丢入runnable接口实现类,调用start()方法
public class TestThread3 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println("线程" + i);
}
}
public static void main(String[] args) {
//创建runnable接口的实现类对象
TestThread3 testThread3 = new TestThread3();
//创建线程对象,通过线程对象来开启我们的线程,代理
// Thread thread = new Thread(testThread3);
// thread.start();
new Thread(testThread3).start();
for (int i = 0; i < 20; i++) {
System.out.println("学习" +i);
}
}
}
3. 实现Callable接口(了解)
1)创建一个类实现 Callable 接口 ,重写 call 方法
2) 创建目标对象
3) 创建一个执行服务
4)提交执行
5)获取返回结果
6)关闭执行服务
//实现一个类,重写call()方法,
public class TestCallable implements Callable<Boolean> {
@Override
public Boolean call() {
for (int i = 0; i < 20; i++) {
System.out.println(Thread.currentThread().getName() + "在上班-----" + i);
}
return Boolean.TRUE;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
TestCallable t1 = new TestCallable();
TestCallable t2 = new TestCallable();
TestCallable t3 = new TestCallable();
//创建执行服务
ExecutorService executorService = Executors.newFixedThreadPool(3);
//提交执行
Future<Boolean> result1 = executorService.submit(t1);
Future<Boolean> result2 = executorService.submit(t2);
Future<Boolean> result3 = executorService.submit(t3);
//获取结果
Boolean r1 = result1.get();
Boolean r2 = result2.get();
Boolean r3 = result3.get();
//关闭服务
executorService.shutdownNow();
}
}
边栏推荐
- How to configure phpunit under window
- 第2章-2 计算分段函数[1]
- 竞赛:糖尿病遗传风险检测挑战赛(科大讯飞)
- ASP. Net core foundation V
- Blog building 7: Hugo
- (13) Simple temperature alarm device based on 51 single chip microcomputer
- Wechat applet - wechat applet browsing PDF files
- The five pictures tell you: why is there such a big gap between people in the workplace?
- When will brain like intelligence, which is popular in academia, land? Let's listen to what the industry masters say - qubits, colliders, x-knowledge Technology
- SQL注入 ----前置基础
猜你喜欢

HCIP第八天
![[Qt5] small software with 5 people randomly selected from the bid evaluation expert base](/img/ca/9f6bd6af45e2113c050edf3a65aaf2.png)
[Qt5] small software with 5 people randomly selected from the bid evaluation expert base

HCIP---LDP和MPLS技术(详解)

See how Google uses pre training weights in target detection tasks | CVPR 2022

【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样

PMP practice once a day | don't get lost in the exam -7.13

2022 Niuke multi school second problem solving Report

Analysis and recurrence of network security vulnerabilities

业务数字化飞速奔跑,管理数字化亟待出发

百度智能云九州区县大脑,描绘城乡新蓝图!
随机推荐
leetcode/数组中和为0的三个不同数
ciou损失
How to configure phpunit under window
sql server时间字段排序
2022牛客多校第二场解题报告
VK1620温控仪/智能电表LED数显驱动芯片3/4线接口内置 RC振荡器,提供技术支持
机器学习如何做到疫情可视化——疫情数据分析与预测实战
Gbase appears in Unicom cloud Tour (Sichuan Station) to professionally empower cloud ecology
NDK 系列(6):说一下注册 JNI 函数的方式和时机
uniapp---- 获取当前位置的经纬度等信息的详细步骤(包含小程序)
postgresql查询【表字段类型】和库中【所有序列】
MySQL how to add users and set permissions?
C轮融资已完成!思迈特软件领跑国内BI生态赋能,产品、服务竿头一步
Simple use of unity queue
GBase 8a MPP与银河麒麟(x86版)完成深度适配
五张图看懂EMI电磁干扰的传播过程-方波陡峭程度对高频成分的影响,时序到频域频谱图形,波形形状对EMI辐射的影响。
中标捷报!南大通用GBase 8s中标南瑞集团2022年数据库框架项目
Unity中队列(Queue)的简单使用
leetcode/单词长度的最大乘积
解决:IndexError: index 13 is out of bounds for dimension 0 with size 13