Future pattern

本文介绍Future模式的应用场景及实现方式,并提供一个Java并发编程示例,演示如何使用FutureTask来处理耗时任务而不阻塞主线程。
个人理解 Future 模式就是在主线程中当需要进行比较耗时的作业,但不想阻塞主线程的作业时,将耗时作业交由 Future 对象在后台中完成,当主线程将来(这个 Future 的意义也就体现在这里了)需要时即可通过 Future 对象获得已经作业对象。

这里写了一个简单的例子来说明这种模式,其实写这个例子主要是自己想熟悉一下 JDK5 的 concurrency 包中 FutureTask 的用法了。例子模拟的是一个会计算账的过程,主线程中已经获得其他帐户的总额了,为了不让主线程等待 PrivateAccount 返回而启用新的线程去处理,并使用 FutureTask 对象来监控,最后需要计算总额的时候再尝试去获得 PrivateAccount 的信息。



代码如下:

1 package testCallable;
2
3 import java.util.Random;
4 import java.util.concurrent.Callable;
5 import java.util.concurrent.ExecutionException;
6 import java.util.concurrent.FutureTask;
7
8 /** */ /**
9 * @author chenpengyi
10 */
11 public class SumAccountExample {
12
13 public static void main(String[] args) {
14 // Init callable object and future task
15 Callable pAccount = new PrivateAccount();
16 FutureTask futureTask = new FutureTask(pAccount);
17
18 // Create a new thread to do so
19 Thread pAccountThread = new Thread(futureTask);
20 pAccountThread.start();
21
22 // Do something else in the main thread
23 System.out.println( " Doing something else here. " );
24
25 // Get the total money from other accounts
26 int totalMoney = new Random().nextInt( 100000 );
27 System.out.println( " You have " + totalMoney + " in your other Accounts. " );
28 System.out.println( " Waiting for data from Private Account " );
29 // If the Future task is not finished, we will wait for it
30 while ( ! futureTask.isDone()) {
31 try {
32 Thread.sleep( 5 );
33 } catch (InterruptedException e) {
34 e.printStackTrace();
35 }
36 }
37 Integer privataAccountMoney = null ;
38 // Since the future task is done, get the object back
39 try {
40 privataAccountMoney = (Integer)futureTask.get();
41 } catch (InterruptedException e) {
42 e.printStackTrace();
43 } catch (ExecutionException e) {
44 e.printStackTrace();
45 }
46 System.out.println( " The total moeny you have is " + (totalMoney + privataAccountMoney.intValue()));
47 }
48
49 }
50
51
52 class PrivateAccount implements Callable {
53
54 Integer totalMoney;
55
56 @Override
57 public Integer call() throws Exception {
58 // Simulates a time conusimg task, sleep for 10s
59 Thread.sleep( 10000 );
60 totalMoney = new Integer( new Random().nextInt( 10000 ));
61 System.out.println( " You have " + totalMoney + " in your private Account. " );
62 return totalMoney;
63 }
64
65 }
主线程获得了返回后即完成了总额的计算。
【四旋翼无人机】具备螺旋桨倾斜机构的全驱动四旋翼无人机:建模与控制研究(Matlab代码、Simulink仿真实现)内容概要:本文围绕具备螺旋桨倾斜机构的全驱动四旋翼无人机展开研究,重点探讨其系统建模与控制策略,结合Matlab代码与Simulink仿真实现。文章详细分析了无人机的动力学模型,特别是引入螺旋桨倾斜机构后带来的全驱动特性,使其在姿态与位置控制上具备更强的机动性与自由度。研究涵盖了非线性系统建模、控制器设计(如PID、MPC、非线性控制等)、仿真验证及动态响应分析,旨在提升无人机在复杂环境下的稳定性和控制精度。同时,文中提供的Matlab/Simulink资源便于读者复现实验并进一步优化控制算法。; 适合人群:具备一定控制理论基础和Matlab/Simulink仿真经验的研究生、科研人员及无人机控制系统开发工程师,尤其适合从事飞行器建模与先进控制算法研究的专业人员。; 使用场景及目标:①用于全驱动四旋翼无人机的动力学建模与仿真平台搭建;②研究先进控制算法(如模型预测控制、非线性控制)在无人机系统中的应用;③支持科研论文复现、课程设计或毕业课题开发,推动无人机高机动控制技术的研究进展。; 阅读建议:建议读者结合文档提供的Matlab代码与Simulink模型,逐步实现建模与控制算法,重点关注坐标系定义、力矩分配逻辑及控制闭环的设计细节,同时可通过修改参数和添加扰动来验证系统的鲁棒性与适应性。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值