当前位置:网站首页>Three ways to create threads
Three ways to create threads
2022-07-28 08:44:00 【TPH-BETTER】
Create thread of 3 Ways of planting
from https://blog.csdn.net/weixin_49920327/article/details/123561378
1. Inherit Thread class
**1) Create a class , Inherit Thread And rewrite run Method **
2) Create objects perform start Method
// Create thread mode one : Inherit Thread class , rewrite run() Method , call start() Open thread
public class TestThread1 extends Thread{
@Override
public void run() {
//run() Method thread body
for (int i = 0; i < 20; i++) {
System.out.println(" I'm looking at the code ——————" +i);
}
}
public static void main(String[] args) {
//main() Threads , The main thread
// Create an object
TestThread1 testThread1 = new TestThread1();
// call start() Method
testThread1.start();
for (int i = 0; i < 20; i++) {
System.out.println(" I'm learning multithreading " + i);
}
}
}
2. Realization Runnable Interface
1) Create a class Realization Runnable Interface and Implementation run Method
2) Create a This kind object
3) Create a Thread object , The input parameters of the construction method are This kind object
4)Thread object perform start Method
// Create thread mode 2: Realization runnable Interface , rewrite run Method , The execution thread drops runnable Interface implementation class , call start() Method
public class TestThread3 implements Runnable{
@Override
public void run() {
for (int i = 0; i < 20; i++) {
System.out.println(" Threads " + i);
}
}
public static void main(String[] args) {
// establish runnable Implementation class object of interface
TestThread3 testThread3 = new TestThread3();
// Creating thread objects , Open our thread through thread object , agent
// Thread thread = new Thread(testThread3);
// thread.start();
new Thread(testThread3).start();
for (int i = 0; i < 20; i++) {
System.out.println(" Study " +i);
}
}
}
3. Realization Callable Interface ( understand )
1) Create a class implementation Callable Interface , rewrite call Method
2) Create target object
3) Create an execution service
4) Submit for execution
5) Get the return result
6) Close execution service
// Implement a class , rewrite call() Method ,
public class TestCallable implements Callable<Boolean> {
@Override
public Boolean call() {
for (int i = 0; i < 20; i++) {
System.out.println(Thread.currentThread().getName() + " at work -----" + i);
}
return Boolean.TRUE;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
TestCallable t1 = new TestCallable();
TestCallable t2 = new TestCallable();
TestCallable t3 = new TestCallable();
// Create execution service
ExecutorService executorService = Executors.newFixedThreadPool(3);
// Submit for execution
Future<Boolean> result1 = executorService.submit(t1);
Future<Boolean> result2 = executorService.submit(t2);
Future<Boolean> result3 = executorService.submit(t3);
// To get the results
Boolean r1 = result1.get();
Boolean r2 = result2.get();
Boolean r3 = result3.get();
// Close the service
executorService.shutdownNow();
}
}
边栏推荐
- How to import and export Youxuan database
- Three different numbers with 0 in leetcode/ array
- HCIP第九天_BGP实验
- GB/T 41479-2022信息安全技术 网络数据处理安全要求 导图概览
- Solution: indexerror: index 13 is out of bounds for dimension 0 with size 13
- Mysql-怎么添加用户和设置权限?
- Smartbi of smart smart smart software completed the c-round financing and accelerated the domestic Bi into the intelligent era
- [pyqt] pyqt development experience_ How to find events and methods of controls
- 半桥BUCK电路—记录篇
- Simple use of unity queue
猜你喜欢

谷歌 Material Design 的文本框为什么没人用?

中标捷报!南大通用GBase 8s中标南瑞集团2022年数据库框架项目

竞赛:糖尿病遗传风险检测挑战赛(科大讯飞)

Solution: indexerror: index 13 is out of bounds for dimension 0 with size 13

Smartbi of smart smart smart software completed the c-round financing and accelerated the domestic Bi into the intelligent era

【MindSpore易点通机器人-01】你也许见过很多知识问答机器人,但这个有点不一样

Customer first | domestic Bi leader, smart software completes round C financing

When unity switches to another scene, he finds that the scene is dimmed

HCIP第八天

Gbase appears in Unicom cloud Tour (Sichuan Station) to professionally empower cloud ecology
随机推荐
2022牛客多校第二场解题报告
uniapp---- 获取当前位置的经纬度等信息的详细步骤(包含小程序)
5张图告诉你:同样是职场人,差距怎么这么大?
HCIP第八天
Competition: diabetes genetic risk detection challenge (iFLYTEK)
1w5 words to introduce those technical solutions of distributed system in detail
Opengauss synchronization status query
GB/T 41479-2022信息安全技术 网络数据处理安全要求 导图概览
阿里技术四面+交叉面+HR面,成功拿到offer,双非本科进不了大厂?
Get the clicked line number in qtablewidget
Recruiting talents, gbase high-end talent recruitment in progress
postgresql查询【表字段类型】和库中【所有序列】
Can‘t connect to server on ‘IP‘ (60)
HCIP---LDP和MPLS技术(详解)
tkMapper的使用-超详细
Leetcode brushes questions. I recommend this video of the sister Xueba at station B
思迈特软件Smartbi完成C轮融资,推动国产BI加速进入智能化时代
Mysql-怎么添加用户和设置权限?
Flink Window&Time 原理
阻塞队列LinkedBlockingQueue 源码解析