当前位置:网站首页>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
边栏推荐
猜你喜欢
Logback logger and root
On the multi-level certificate based on OpenSSL, the issuance and management of multi-level Ca, and two-way authentication
倒计时1天~2022云容灾产品线上发布会即将开始
帆软实现一个单选按钮,可以统一设置其他单选按钮的选择状态
MySQL数据库讲解(九)
查询2013年到2021年的数据,只查询到2020的数据,遇到了这个问题所进行的解决办法
01-NIO基础之ByteBuffer和FileChannel
Jmeter安装教程以及我遇到的问题的解决办法
ThreadGroup ThreadGroup implémente l'interface threadfactory en utilisant la classe Introduction + Custom thread Factory
About the configuration and use of json5 in nodejs
随机推荐
Wechat applet input hidden and inoperable settings
Installation and use of postman pit
Jira截取全图
教育行业云迁移最佳实践:海云捷迅使用HyperMotion云迁移产品为北京某大学实施渐进式迁移,成功率100%
Mysql个人学习总结
Wechat applet positioning and ranging through low-power Bluetooth device (2)
文字组合,不重复,做搜索或查询关键字匹配
贷款市场报价利率(LPR)与贷款基准利率介绍
redis数据库讲解(三)redis数据类型
Wechat applet initializes Bluetooth, searches nearby Bluetooth devices and connects designated Bluetooth (I)
Returns the subscript after array sorting
利用json-server在本地创建服务器请求
不同时间类型的执行计划计算
回顾2021:如何帮助客户扫清上云最后一公里的障碍?
Gartner预测云迁移规模大幅增长;云迁移的优势是什么?
MySQL lock database lock
拨开云雾synchronized使用五种方式介绍
Oracle-数据泵使用
帆软中需要设置合计值为0时,一整行都不显示的解决办法
win10自带Groove音乐不能播放CUE和APE文件的一种曲线救国办法,自己创建aimppack插件包,AIMP安装DSP插件