当前位置:网站首页>线程类的几大创建方法
线程类的几大创建方法
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扩展版 按键扫描
- STM32 光敏电阻传感器&两路AD采集
- pytorch神经网络搭建 模板
- Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
- Shell之分析服务器日志命令集锦
- Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
- Research on medical knowledge atlas question answering system (I)
- Pytorch convolution operation
- 解决:拖动xib控件到代码文件中,报错setValue:forUndefinedKey:this class is not key value coding-compliant for the key
- LeetCode_ 35 (search insertion position)
猜你喜欢
VIM easy to use tutorial
The design points of voice dialogue system and the importance of multi round dialogue
About the transmission pipeline of stage in spark
Pytorch(一) —— 基本语法
STM32 extended key scan
最长递增子序列及最优解、动物总重量问题
2022 t elevator repair new version test questions and t elevator repair simulation test question bank
Pytest automated testing - compare robotframework framework
2022 a special equipment related management (elevator) simulation test and a special equipment related management (elevator) certificate examination
RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
随机推荐
Pytorch convolution operation
[FTP] the solution to "227 entering passive mode" during FTP connection
OdeInt與GPU
软件研发的十大浪费:研发效能的另一面
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
LeetCode_53(最大子数组和)
扩展-Fragment
Odeint et GPU
Difficulties in the development of knowledge map & the importance of building industry knowledge map
Leecode records the number of good segmentation of 1525 strings
Pytorch(三) —— 函数优化
Extension fragment
Day 52 - tree problem
STM32 光敏电阻传感器&两路AD采集
C read / write application configuration file app exe. Config and display it on the interface
Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
[FTP] common FTP commands, updating continuously
Basic exercise of test questions hexadecimal to decimal
Basic skeleton of neural network nn Use of moudle
AssertionError assert I.ndim == 4 and I.shape[1] == 3