当前位置:网站首页>创建线程的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();
}
}
边栏推荐
猜你喜欢
![[mindspire YiDianTong robot-01] you may have seen many Knowledge Q & A robots, but this is a little different](/img/d1/c2c2e4a605deddd0073a05d528733f.jpg)
[mindspire YiDianTong robot-01] you may have seen many Knowledge Q & A robots, but this is a little different

NDK 系列(6):说一下注册 JNI 函数的方式和时机

中标捷报!南大通用GBase 8s中标南瑞集团2022年数据库框架项目

Day112.尚医通:手机验证码登录功能

网络安全漏洞分析与漏洞复现
![[Qt5] a method of multi window parameter transmission (using custom signal slot) and case code download](/img/6d/870add6179f0e3a2f9b719f79594f3.png)
[Qt5] a method of multi window parameter transmission (using custom signal slot) and case code download

2022 Niuke multi school second problem solving Report

机器学习如何做到疫情可视化——疫情数据分析与预测实战

解决:IndexError: index 13 is out of bounds for dimension 0 with size 13

Source code analysis of linkedblockingqueue
随机推荐
Viewing vantage's self drive from the "three good" kitchen electricity standard and the value proposition of "serious life"
Characteristics of EMC EMI beads
The fourth phase (2021-2022) research on the implementation of cloud native technology in traditional industries - central state-owned enterprises was officially released
微信小程序----微信小程序浏览pdf文件
【软考软件评测师】2013综合知识历年真题
GBase 8s是否支持存储关系型数据和对象型数据?
Understand the propagation process of EMI electromagnetic interference through five diagrams - the influence of square wave steepness on high-frequency components, the spectrum graph from time sequenc
Allure use
Shell编程规范与变量
[reprint] man Rsync translation (Chinese Manual of Rsync command)
HCIP第九天_BGP实验
QT 怎么删除布局里的所有控件?
Can‘t connect to server on ‘IP‘ (60)
leetcode/数组中和为0的三个不同数
Does gbase 8s support storing relational data and object-oriented data?
Analysis of model predictive control (MPC) (IX): numerical solution of quadratic programming (II)
Introduction to self drive tour of snow mountains in the West in January 2018
Can‘t connect to server on ‘IP‘ (60)
ASP. Net core foundation IV
‘全局事件总线’&‘消息订阅与发布’