当前位置:网站首页>快速搞懂线程实现的三种方式
快速搞懂线程实现的三种方式
2022-04-23 14:07:00 【pureluckyfish】
一、实现线程的三种方式:
方式一、继承Thread类,复写run()方法
class AThread extends Thread {
@Override
public void run() {
System.out.println("AThread:" + Thread.currentThread().getName());
super.run();
}
}
方式二、实现Runnable接口,复写run()方法
class BThread implements Runnable {
@Override
public void run() {
System.out.println("BThread:" + Thread.currentThread().getName());
}
}
方式三、实现Callable接口,复写call()方法
class CThread implements Callable<Object> {
@Override
public Object call() throws Exception {
System.out.println("CThread:" + Thread.currentThread().getName());
return Thread.currentThread().getName();
}
}
二、线程执行
package ThreadStudy;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
public class T1 {
public static void main(String[] args) throws Exception {
// 方式一实现
AThread t1 = new AThread();
t1.start();
// 方式二实现(一)
Thread t2 = new Thread(new BThread());
t2.start();
// 方式二实现(二):匿名内部类
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("main:" + Thread.currentThread().getName());
}
});
t3.start();
// 方式三实现(一):线程池中执行Callable线程获取返回值
ExecutorService exec = Executors.newSingleThreadExecutor();
CThread ct1 = new CThread();
Future f1 = exec.submit(ct1);
System.out.println("方式三实现(一):" + f1.get());
// 方式三实现(二):FutureTask类中执行Callable线程获取返回值
CThread ct2 = new CThread();
FutureTask<Object> f2 = new FutureTask<Object>(ct2);
exec.submit(f2);
System.out.println("方式三实现(二):" + f2.get());
exec.shutdown();
}
}
//方式一、继承Thread类,复写run()方法
class AThread extends Thread {
@Override
public void run() {
System.out.println("AThread:" + Thread.currentThread().getName());
super.run();
}
}
//方式二、实现Runnable接口,复写run()方法
class BThread implements Runnable {
@Override
public void run() {
System.out.println("BThread:" + Thread.currentThread().getName());
}
}
//方式三、实现Callable接口,复写call()方法
class CThread implements Callable<Object> {
@Override
public Object call() throws Exception {
System.out.println("CThread:" + Thread.currentThread().getName());
return Thread.currentThread().getName();
}
}
三、Runnable执行成功后指定返回值
可以指定任意类型的返回结果 | <T> Future<T> submit(Runnable task, T result); |
代码实现
package ThreadStudy;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class TestTmp {
public static void main(String[] args) throws Exception {
ExecutorService exec = Executors.newSingleThreadExecutor();
Future future = exec.submit(new Runnable() {
@Override
public void run() {
System.out.println("Runnable开始执行");
}
}, "success");
System.out.println("获取Runnable执行之后的返回结果:" + future.get());
}
}
四、Callable与Runnable区别:
Runnable | Callable | |
1、复写方法不同 | run() | call() |
2、是否带返回值 | 否 | 是 |
3、执行方式不同 | 既能在线程池中执行 又能在Thread类中执行 |
只能放在线程池中执行 |
版权声明
本文为[pureluckyfish]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sinat_33918956/article/details/122426251
边栏推荐
- 1到100号的灯开关问题
- 基于CM管理的CDH集群集成Phoenix
- Understand the concepts of virtual base class, virtual function and pure virtual function (turn)
- win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件
- org.apache.parquet.schema.InvalidSchemaException: A group type can not be empty. Parquet does not su
- Homebrew是什么?以及使用
- DeepinV20安装Mariadb
- There is a mining virus in the server
- mysql 5.1升级到5.610
- 预览CSV文件
猜你喜欢
随机推荐
Operation instructions of star boundary text automatic translator
redis数据库讲解(四)主从复制、哨兵、Cluster群集
什么是云迁移?云迁移的四种模式分别是?
Intégration de Clusters CDH Phoenix basée sur la gestion cm
ActiveMq基础知识
leetcode--380. O (1) time insertion, deletion and acquisition of random elements
关于密匙传递的安全性和数字签名
报表FCRA考试题集及答案(错了11题)
帆软报表设置单元格填报以及根据值的大小进行排名方法
Research on recyclerview details - Discussion and repair of recyclerview click dislocation
PySide2
帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态
关于云容灾,你需要知道这些
RobotFramework 之 文件上传和下载
Jacob print word
Multiple inheritance virtual base exercises
Indoor and outdoor map switching (indoor three-point positioning based on ibeacons)
RobotFramework 之 用例执行
关于Jmeter启动闪退问题
win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件