当前位置:网站首页>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
边栏推荐
- Wechat applet communicates with low-power Bluetooth - sending data to hardware (III)
- About the configuration and use of json5 in nodejs
- Some good articles on pthread multithreading
- 查询2013年到2021年的数据,只查询到2020的数据,遇到了这个问题所进行的解决办法
- 在MAC上安装mysql
- Gartner预测云迁移规模大幅增长;云迁移的优势是什么?
- VMware15Pro在Deepin系统里面挂载真机电脑硬盘
- VMware 15pro mounts the hard disk of the real computer in the deepin system
- 微信小程序客服接入,实现发送和接收消息
- MySQL数据库讲解(八)
猜你喜欢
随机推荐
困扰多年的系统调研问题有自动化采集工具了,还是开源免费的
Operation instructions of star boundary text automatic translator
mysql 5.1升级到5.67
字节面试编程题:最小的K个数
MySQL数据库讲解(十)
logback-logger和root
mysql 5.1升级到5.610
Wechat applet positioning and ranging through low-power Bluetooth device (2)
Get the thread return value. Introduction to the use of future interface and futuretask class
帆软实现分页时第一行和最后两行冻结方式
云容灾是什么意思?云容灾和传统容灾的区别?
Can global variables be defined in header files
帆软中使用if else 进行判断-使用标题条件进行判断
第四届“传智杯”全国大学生IT技能大赛(决赛B组) 题解
dp-[NOIP2000]方格取数
Oracle-数据泵使用
关于Jmeter启动闪退问题
Date的after时间判断
Storage path of mod subscribed by starbound Creative Workshop at Star boundary
政务云迁移实践 北明数科使用HyperMotion云迁移产品为某政府单位实施上云迁移项目,15天内完成近百套主机迁移