当前位置:网站首页>Quickly understand the three ways of thread implementation
Quickly understand the three ways of thread implementation
2022-04-23 14:15:00 【pureluckyfish】
One 、 Three ways to implement threads :
Mode one 、 Inherit Thread class , make carbon copies run() Method
class AThread extends Thread {
@Override
public void run() {
System.out.println("AThread:" + Thread.currentThread().getName());
super.run();
}
}
Mode two 、 Realization Runnable Interface , make carbon copies run() Method
class BThread implements Runnable {
@Override
public void run() {
System.out.println("BThread:" + Thread.currentThread().getName());
}
}
Mode three 、 Realization Callable Interface , make carbon copies call() Method
class CThread implements Callable<Object> {
@Override
public Object call() throws Exception {
System.out.println("CThread:" + Thread.currentThread().getName());
return Thread.currentThread().getName();
}
}
Two 、 Threads execute
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 {
// The first way is to realize
AThread t1 = new AThread();
t1.start();
// Mode 2 implementation ( One )
Thread t2 = new Thread(new BThread());
t2.start();
// Mode 2 implementation ( Two ): Anonymous inner class
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("main:" + Thread.currentThread().getName());
}
});
t3.start();
// Mode 3 Implementation ( One ): Execution in thread pool Callable The thread gets the return value
ExecutorService exec = Executors.newSingleThreadExecutor();
CThread ct1 = new CThread();
Future f1 = exec.submit(ct1);
System.out.println(" Mode 3 Implementation ( One ):" + f1.get());
// Mode 3 Implementation ( Two ):FutureTask Class to perform Callable The thread gets the return value
CThread ct2 = new CThread();
FutureTask<Object> f2 = new FutureTask<Object>(ct2);
exec.submit(f2);
System.out.println(" Mode 3 Implementation ( Two ):" + f2.get());
exec.shutdown();
}
}
// Mode one 、 Inherit Thread class , make carbon copies run() Method
class AThread extends Thread {
@Override
public void run() {
System.out.println("AThread:" + Thread.currentThread().getName());
super.run();
}
}
// Mode two 、 Realization Runnable Interface , make carbon copies run() Method
class BThread implements Runnable {
@Override
public void run() {
System.out.println("BThread:" + Thread.currentThread().getName());
}
}
// Mode three 、 Realization Callable Interface , make carbon copies call() Method
class CThread implements Callable<Object> {
@Override
public Object call() throws Exception {
System.out.println("CThread:" + Thread.currentThread().getName());
return Thread.currentThread().getName();
}
}
3、 ... and 、Runnable Specify the return value after successful execution
| You can specify any type of return result | <T> Future<T> submit(Runnable task, T result); |
Code implementation
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 Start execution ");
}
}, "success");
System.out.println(" obtain Runnable The returned result after execution :" + future.get());
}
}
Four 、Callable And Runnable difference :
| Runnable | Callable | |
| 1、 Different replication methods | run() | call() |
| 2、 With or without return value | no | yes |
| 3、 Different execution methods | It can execute in the process pool Can be in again Thread Class to perform |
Can only be executed in the thread pool |
版权声明
本文为[pureluckyfish]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231406486054.html
边栏推荐
猜你喜欢

線程組ThreadGroup使用介紹+自定義線程工廠類實現ThreadFactory接口

帆软实现分页时第一行和最后两行冻结方式

MySQL数据库讲解(七)

Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)

回顾2021:如何帮助客户扫清上云最后一公里的障碍?

VMware15Pro在Deepin系统里面挂载真机电脑硬盘

Storage path of mod subscribed by starbound Creative Workshop at Star boundary

帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态

How QT designer adds resource files

倒计时1天~2022云容灾产品线上发布会即将开始
随机推荐
百度图片识别自定义实现(替代AipOcr)
Storage path of mod subscribed by starbound Creative Workshop at Star boundary
Essential difference between restful WebService and gSOAP webservice
帆软调用动态传参的方法,在标题中设置参数
预览CSV文件
OpenStack命令操作
帆软之单元格部分字体变颜色
ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory
Homebrew是什么?以及使用
mysql 5.1升级到5.66
GFS分布式文件系统(理论)
RecyclerView进阶使用-实现仿支付宝菜单编辑页面拖拽功能
Win10 comes with groove music, which can't play cue and ape files. It's a curvilinear way to save the country. It creates its own aimpack plug-in package, and aimp installs DSP plug-in
HyperMotion云迁移助力中国联通,青云完成某央企上云项目,加速该集团核心业务系统上云进程
帆软中根据分类进行汇总
剑指offer刷题(1)--面向华为
多云数据流转?云上容灾?年前最后的价值内容分享
mysql 5.1升级到5.611
Algorithem_ReverseLinkedList
如何快速批量创建文本文档?