当前位置:网站首页>创建线程的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();
}
}
边栏推荐
猜你喜欢

Mysql-怎么添加用户和设置权限?

CAT1 4g+ Ethernet development board 232 data is sent to the server through 4G module TCP

Matlab file path

MySQL how to add users and set permissions?

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

CAT1 4G+以太网开发板232数据通过4G模块TCP发到服务器

Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始

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

How to build the protection system of class protection technology of 2022 series of ISO compliance (Part I)

NDK 系列(6):说一下注册 JNI 函数的方式和时机
随机推荐
opencv+paddle orc 识别图片提取表格信息
VK1620温控仪/智能电表LED数显驱动芯片3/4线接口内置 RC振荡器,提供技术支持
HCIP第九天_BGP实验
uniapp上下滑屏切换支持视频和图片轮播实现,类似抖音效果
Simple use of unity queue
Machine learning how to achieve epidemic visualization -- epidemic data analysis and prediction practice
[Qt5] small software with 5 people randomly selected from the bid evaluation expert base
ASP. Net core foundation VIII
GBase 8s是否支持存储关系型数据和对象型数据?
leetcode/数组中和为0的三个不同数
一键开关机电路
Recommend a fully open source, feature rich, beautiful interface mall system
解决:IndexError: index 13 is out of bounds for dimension 0 with size 13
Window 2 - > toolbar (28-1)
二维数组及操作
How to write a JMeter script common to the test team
Kubernetes技术与架构(七)
2022牛客多校第一场解题报告
File operation of QT
Characteristics of EMC EMI beads