当前位置:网站首页>Several methods of creating thread classes
Several methods of creating thread classes
2022-07-01 04:48:00 【Yangasang 815】
import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
public class Test1 {
public static void main(String[] args) throws Exception {
// Create method 1 Create thread class inheritance Thread class
MyThread m1=new MyThread();
m1.start();
// Create a thread method 2; Create a thread task class MyRunable Realization Runable Interface take Runable Object to Thread Handle
MyRunable m2=new MyRunable();
Thread mm2=new Thread(m2);
mm2.start();
// establish Runable Anonymous inner class objects are given to Thread
Runnable runnable=new Runnable() {
@Override
public void run() {
System.out.println(" Sub thread 3 starts running ");
}
};
new Thread(runnable).start();
// Method 3 : utilize Callable And FutureTask Interface implementation
MyCallable callable=new MyCallable();
FutureTask futureTask=new FutureTask(callable);
new Thread(futureTask).start();
System.out.println(futureTask.get());
}
}
class MyThread extends Thread{
@Override
public void run(){
System.out.println(" Sub thread 1 starts running ");
}
}
class MyRunable implements Runnable{
@Override
public void run() {
System.out.println(" Sub thread 2 starts running ");
}
}
class MyCallable implements Callable{
@Override
public Object call() throws Exception {
return " Sub thread 4 starts running ";
}
}边栏推荐
- pytorch 卷积操作
- How do I sort a list of strings in dart- How can I sort a list of strings in Dart?
- js解决浮点数相乘精度丢失问题
- STM32 photoresistor sensor & two channel AD acquisition
- Fitness without equipment
- [difficult] sqlserver2008r2, can you recover only some files when recovering the database?
- 分布式数据库数据一致性的原理、与技术实现方案
- Pytoch (III) -- function optimization
- Dual contractual learning: text classification via label aware data augmentation reading notes
- Query long transaction
猜你喜欢

Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)

Use and modification of prior network model

How to do the performance pressure test of "Health Code"

Applications and features of VR online exhibition

分布式全局唯一ID解决方案详解

Difficulties in the development of knowledge map & the importance of building industry knowledge map

神经网络-使用Sequential搭建神经网络

Pytest automated testing - compare robotframework framework

PR 2021 quick start tutorial, learn about the and functions of the timeline panel

Research on medical knowledge atlas question answering system (I)
随机推荐
Applications and features of VR online exhibition
洗个冷水澡吧
2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank
【暑期每日一题】洛谷 P1568 赛跑
Leecode record 1351 negative numbers in statistical ordered matrix
Leecode question brushing record 1332 delete palindrome subsequence
Quelques outils dont les chiens scientifiques pourraient avoir besoin
分布式事务-解决方案
[pat (basic level) practice] - [simple simulation] 1064 friends
线程安全问题
Codeworks round 449 (Div. 1) C. Kodori tree template
[hard ten treasures] - 1 [basic knowledge] classification of power supply
LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver
LeetCode_ 53 (maximum subarray and)
LeetCode_ 28 (implement strstr())
LeetCode_ 58 (length of last word)
STM32 extended key scan
[summer daily question] Luogu p5886 Hello, 2020!
How to do the performance pressure test of "Health Code"
Shell analysis server log command collection