当前位置:网站首页>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();
}
}
边栏推荐
- Simple use of unity queue
- 1w5字详细介绍分布式系统的那些技术方案
- 机器学习如何做到疫情可视化——疫情数据分析与预测实战
- 【软考软件评测师】2013综合知识历年真题
- Matlab file path
- Export SQL server query results to excel table
- oracle sql 问题
- Vk1620 temperature controller / smart meter LED digital display driver chip 3/4-wire interface with built-in RC oscillator to provide technical support
- 2022 Niuke multi school second problem solving Report
- Wechat applet - wechat applet browsing PDF files
猜你喜欢

【OpenCV】生成透明的PNG图像

Half bridge buck circuit - record

Leetcode brushes questions. I recommend this video of the sister Xueba at station B

我来教你如何组装一个注册中心?

Ciou loss

第2章-14 求整数段和

leetcode刷题,我推荐B站这个妹子学霸的视频

Source code analysis of linkedblockingqueue

Can‘t connect to server on ‘IP‘ (60)

Use of tkmapper - super detailed
随机推荐
MySQL how to add users and set permissions?
Recruiting talents, gbase high-end talent recruitment in progress
ASP. Net core foundation IV
5张图告诉你:同样是职场人,差距怎么这么大?
Sliding screen switching on uniapp supports video and image rotation, similar to Tiktok effect
客户至上 | 国产BI领跑者,思迈特软件完成C轮融资
Gb/t 41479-2022 information security technology network data processing security requirements map overview
HCIP---LDP和MPLS技术(详解)
Maximum product of leetcode/ word length
2022牛客多校第二场解题报告
网络安全漏洞分析与漏洞复现
Get the clicked line number in qtablewidget
1w5 words to introduce those technical solutions of distributed system in detail
Matlab file path
Alibaba internal interview materials
sparksql 与flinksql 建表 与 连表记录
Creation of status bar (29)
Flink Window&Time 原理
PostgreSQL queries [table field type] and [all series] in the library
置顶各大平台,22版面试核心知识解析笔记,强势上榜