当前位置:网站首页>线程类的几大创建方法
线程类的几大创建方法
2022-07-01 04:46:00 【洋啊桑815】
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class Test1 {
public static void main(String[] args) throws Exception {
//创建方法1 创建线程类继承Thread类
MyThread m1=new MyThread();
m1.start();
//创建线程方法2;创建线程任务类MyRunable实现Runable接口 将Runable对象交给Thread处理
MyRunable m2=new MyRunable();
Thread mm2=new Thread(m2);
mm2.start();
//创建Runable匿名内部类对象交给Thread
Runnable runnable=new Runnable() {
@Override
public void run() {
System.out.println("子线程三号开始跑动");
}
};
new Thread(runnable).start();
//方法三:利用Callable与FutureTask接口实现
MyCallable callable=new MyCallable();
FutureTask futureTask=new FutureTask(callable);
new Thread(futureTask).start();
System.out.println(futureTask.get());
}
}
class MyThread extends Thread{
@Override
public void run(){
System.out.println("子线程一号开始跑动");
}
}
class MyRunable implements Runnable{
@Override
public void run() {
System.out.println("子线程二号开始跑动");
}
}
class MyCallable implements Callable{
@Override
public Object call() throws Exception {
return "子线程四号开始跑动";
}
}边栏推荐
- 分布式事务-解决方案
- Pytest automated testing - compare robotframework framework
- 无器械健身
- 分布式锁的实现
- [ue4] event distribution mechanism of reflective event distributor and active call event mechanism
- 2022 question bank and answers for safety production management personnel of hazardous chemical production units
- 神经网络-使用Sequential搭建神经网络
- How to do the performance pressure test of "Health Code"
- Construction of Meizhou nursing laboratory: equipment configuration
- 科研狗可能需要的一些工具
猜你喜欢

Simple implementation of slf4j

Dataloader的使用

Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor

LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
![[pat (basic level) practice] - [simple simulation] 1064 friends](/img/37/0ef0f8aae15ae574be1d76c97497c9.jpg)
[pat (basic level) practice] - [simple simulation] 1064 friends

Odeint and GPU

2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis

Pytorch(四) —— 可视化工具 Visdom

How to use common datasets in pytorch

RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
随机推荐
Use of dataloader
细数软件研发效能的七宗罪
分布式事务-解决方案
LeetCode_58(最后一个单词的长度)
Summary of acl2021 information extraction related papers
【FTP】FTP常用命令,持续更新中……
2022 question bank and answers for safety production management personnel of hazardous chemical production units
Solve the problem that the external chain file of Qiankun sub application cannot be obtained
2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
Pytorch(一) —— 基本语法
LeetCode_ 53 (maximum subarray and)
2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
This sideline workload is small, 10-15k, free unlimited massage
Day 52 - tree problem
Shell之分析服务器日志命令集锦
Construction of Meizhou nursing laboratory: equipment configuration
常用的Transforms中的方法
Research on medical knowledge atlas question answering system (I)
AssertionError assert I.ndim == 4 and I.shape[1] == 3
STM32扩展版 按键扫描