当前位置:网站首页>线程类的几大创建方法
线程类的几大创建方法
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 "子线程四号开始跑动";
}
}边栏推荐
- STM32 光敏电阻传感器&两路AD采集
- Pytorch(一) —— 基本语法
- STM32 extended key scan
- 解决qiankun中子应用外链文件无法获取
- How to use maixll dock
- 2022 polymerization process test questions and simulation test
- JVM栈和堆简介
- Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
- 2022 t elevator repair question bank and simulation test
- AssertionError assert I.ndim == 4 and I.shape[1] == 3
猜你喜欢
![[ue4] event distribution mechanism of reflective event distributor and active call event mechanism](/img/44/6a26ad24d56ddd5156f3a31fa7e0b9.jpg)
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism

分布式架构系统拆分原则、需求、微服务拆分步骤

JVM栈和堆简介

先有网络模型的使用及修改

Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom

STM32扩展版 按键扫描

扩展-Fragment

2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination

Neural network convolution layer

STM32扩展板 温度传感器和温湿度传感器的使用
随机推荐
VR线上展览所具备应用及特色
LeetCode_35(搜索插入位置)
Basic skeleton of neural network nn Use of moudle
Odeint and GPU
How do I sort a list of strings in dart- How can I sort a list of strings in Dart?
Construction of Meizhou nursing laboratory: equipment configuration
The index is invalid
JS rotation chart
RDF query language SPARQL
Day 52 - tree problem
TCP server communication flow
神经网络-非线性激活
分布式锁的实现
细数软件研发效能的七宗罪
Overview of the construction details of Meizhou veterinary laboratory
LeetCode_28(实现 strStr())
Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
LeetCode_66(加一)
Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
Shell analysis server log command collection