当前位置:网站首页>子父线程交互
子父线程交互
2022-07-28 05:16:00 【小疯子青】
public static void main(String[] args) throws Exception {
FutureTask futureTask1 = new FutureTask(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(3000);
System.out.println(Thread.currentThread().getName() + "---------哈哈哈 1------");
return Thread.currentThread().getName() + "---------hello 1------";
}
});
FutureTask futureTask2 = new FutureTask(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(3000000);
System.out.println(Thread.currentThread().getName() + "---------哈哈哈 2------");
// throw new Exception();
return "test2";
}
});
new Thread(futureTask1).start();
new Thread(futureTask2).start();
System.out.println(futureTask1.get());
try {
futureTask2.get(1,TimeUnit.NANOSECONDS);
}catch (TimeoutException e) {
futureTask2.cancel(true);
e.printStackTrace();
}catch (Exception e){
System.out.println(e.getMessage());
}
}
运行结果
Thread-0---------哈哈哈 1------
Thread-0---------hello 1------
java.util.concurrent.TimeoutException
at java.util.concurrent.FutureTask.get(FutureTask.java:205)
at question.ExceptionInChildThread.main(ExceptionInChildThread.java:79)
边栏推荐
- Reading notes of SMT practical guide 1
- Know etcd
- 【CVPR2022】Lite Vision Transformer with Enhanced Self-Attention
- Tips for using swiper (1)
- 使用nfpm制作rpm包
- FreeRTOS startup process, coding style and debugging method
- 7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格
- 多御安全浏览器将改进安全模式,让用户浏览更安全
- Message forwarding mechanism -- save your program from crashing
- The most detailed installation of windows10 virtual machine, install virtual machine by hand, and solve the problem that the Hyper-V option cannot be found in the home version window
猜你喜欢

SSLError

【CVPR2022】Lite Vision Transformer with Enhanced Self-Attention

【CVPR2022】On the Integration of Self-Attention and Convolution

flink思维导图

Flask Development & get/post request

【CVPR2022】Multi-Scale High-Resolution Vision Transformer for Semantic Segmentation

Mysql基本查询

Online sql to XML tool

Know etcd

这种动态规划你见过吗——状态机动态规划之股票问题(中)
随机推荐
Keil Chinese garbled code solution
HDU 3666 the matrix problemdifferential constraint + stack optimization SPFA negative ring
21 day SQL punch in summary
ES6 new variable modifiers let and const, new basic data type symbol
What is the reason why the easycvr national standard protocol access equipment is online but the channel is not online?
Anaconda common instructions
Interpretation of afnetworking4.0 request principle
I've been in an outsourcing company for two years, and I feel like I'm going to die
C language characters and strings
HDU 1435 stable match
HashSet add
HDU 3592 World Exhibition (differential constraint)
在ruoyi生成的对应数据库的代码 之后我该怎么做才能做出下边图片的样子
Data imbalance: comprehensive sampling of anti fraud model (data imbalance)
PC side bug record
First acquaintance with C language (1)
11. < tag dynamic programming and subsequence, subarray> lt.115. Different subsequences + Lt. 583. Deletion of two strings DBC
Pipe /createpipe
HDU 3585 maximum shortest distance
Dcgan:deep volume general adaptive networks -- paper analysis