当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢

49-OpenCv深入分析轮廓

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

Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始

思迈特软件Smartbi完成C轮融资,推动国产BI加速进入智能化时代

半桥BUCK电路—记录篇

分布式系统架构理论与组件

Win the bid! Nantah general gbase 8s won the bid for the 2022 database framework project of NARI Group

When will brain like intelligence, which is popular in academia, land? Let's listen to what the industry masters say - qubits, colliders, x-knowledge Technology

招贤纳士,GBASE高端人才招募进行中

49 opencv deep analysis profile
随机推荐
微信小程序----微信小程序浏览pdf文件
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
5张图告诉你:同样是职场人,差距怎么这么大?
Usage of constructors
PHPUnit在Window下如何配置
2022牛客多校第二场解题报告
Round C financing has been completed! Smart software leads domestic Bi ecological empowerment, and products and services are a step forward
Usage of qmap
PostgreSQL queries [table field type] and [all series] in the library
SQL Server查询结果导出到EXCEL表格
思迈特软件Smartbi完成C轮融资,推动国产BI加速进入智能化时代
classLoader加载的class的回收
How to configure phpunit under window
Wechat applet - wechat applet browsing PDF files
Gb/t 41479-2022 information security technology network data processing security requirements map overview
The five pictures tell you: why is there such a big gap between people in the workplace?
uniapp---- 获取当前位置的经纬度等信息的详细步骤(包含小程序)
sparksql 与flinksql 建表 与 连表记录
Vk1620 temperature controller / smart meter LED digital display driver chip 3/4-wire interface with built-in RC oscillator to provide technical support
PostgreSQL:无法更改视图或规则使用的列的类型