当前位置:网站首页>线程类的几大创建方法
线程类的几大创建方法
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 "子线程四号开始跑动";
}
}
边栏推荐
- I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
- LM小型可编程控制器软件(基于CoDeSys)笔记十九:报错does not match the profile of the target
- Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition
- How to do the performance pressure test of "Health Code"
- Take a cold bath
- 1076 Forwards on Weibo
- 神经网络-最大池化的使用
- 总结全了,低代码还需要解决这4点问题
- 神经网络-卷积层
- RDF query language SPARQL
猜你喜欢
Difficulties in the development of knowledge map & the importance of building industry knowledge map
LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver
Why is Internet thinking not suitable for AI products?
分布式-总结列表
神经网络-非线性激活
VIM easy to use tutorial
Cmake selecting compilers and setting compiler options
AssertionError assert I.ndim == 4 and I.shape[1] == 3
LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
随机推荐
How to view the changes and opportunities in the construction of smart cities?
Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling
TCP server communication flow
Basic usage, principle and details of session
VIM简易使用教程
OdeInt与GPU
1076 Forwards on Weibo
Fitness without equipment
[2020 overview] overview of link prediction based on knowledge map embedding
LeetCode_35(搜索插入位置)
如何看待智慧城市建设中的改变和机遇?
Use of dataloader
CF1638E. Colorful operations Kodori tree + differential tree array
Announcement on the list of Guangdong famous high-tech products to be selected in 2021
2022 t elevator repair question bank and simulation test
测量三相永磁同步电机的交轴直轴电感
STM32 光敏电阻传感器&两路AD采集
2022 hoisting machinery command registration examination and hoisting machinery command examination registration
Pytorch convolution operation
PR 2021 quick start tutorial, learn about the and functions of the timeline panel