当前位置:网站首页>线程类的几大创建方法
线程类的几大创建方法
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 "子线程四号开始跑动";
}
}边栏推荐
- 分布式事务-解决方案
- 科研狗可能需要的一些工具
- OdeInt與GPU
- Quelques outils dont les chiens scientifiques pourraient avoir besoin
- CF1638E. Colorful operations Kodori tree + differential tree array
- Overview of the construction details of Meizhou veterinary laboratory
- 【FTP】FTP常用命令,持续更新中……
- 分布式全局唯一ID解决方案详解
- 【硬十宝典】——1.【基础知识】电源的分类
- 2022 t elevator repair question bank and simulation test
猜你喜欢

Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and

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

LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target

Openresty rewrites the location of 302

分布式事务-解决方案

Basic usage, principle and details of session

Neural network convolution layer

分布式锁的实现

Maixll dock quick start

神经网络-卷积层
随机推荐
[2020 overview] overview of link prediction based on knowledge map embedding
神经网络-非线性激活
Odeint and GPU
JS rotation chart
The index is invalid
Software testing needs more and more talents. Why do you still not want to take this path?
Basic usage, principle and details of session
神经网络的基本骨架-nn.Moudle的使用
LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
分布式-总结列表
Leecode question brushing record 1332 delete palindrome subsequence
PR 2021 quick start tutorial, learn about the and functions of the timeline panel
How to use common datasets in pytorch
分布式架构系统拆分原则、需求、微服务拆分步骤
LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
LeetCode_53(最大子数组和)
Use of dataloader
Pytorch(三) —— 函数优化
Talk about testdeploy