打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Spring 3中异步方法调用

其实Begin/End方式+.Net委托可以实现很轻量并类似与Future的异步机制:



//根据部门Id统计部门下所有员工的数量
int GetEmployeeCount(int departmentId)
{
Thread.Sleep(1000);
return 100;
}

//输出员工数量
void OutputEmployeeCount(int employeeCount)
{
Assert.IsTrue(employeeCount == 100);
}


Func<int, int> task = GetEmployeeCount;
int departmentId = 1;
task.BeginInvoke(departmentId, future => OutputEmployeeCount(task.EndInvoke(future)), null);


=============

If there is a lot of request invoking the async method,it will cause OutOfMemoryError.

How to control the thread number in the thread pool?Tks.

=============

2010年01月15日 10:21 "ferendo"的言论
thread number in the thread pool


使用ThreadFixedPool来管理启动的线程。当然,这需要修改Spring框架源码,所以可见,框架本身的定制性也非常重要。不可能一个功能应付所有情况,尤其是其一些之前不被重视的功能,就可能没有预留拓展余地。

真正异步并发策略原理这么做:
使用future实现内置异步API


========

2010年01月14日 13:43 "cmzx3444"的言论
@Async是怎么实现异步的啊,对类没有特别的要求吗,真的是太神奇了 ...



MailUtility 该类通过@Component注释告诉Spring容器该类需要被注册中,通过@Async告诉Spring容器该类需要一个Proxy类,把这个代理类注册到DI容器中,该代理类通过Future机制对SendMail方法进行一些包装,这样就把开发人员从异步编程中解脱出来了!

我是.Net开发人员,不知对Java的这种机制的理解是否正确,欢迎指正!

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
C++11之std::future和std::promise
Java异步编程接口:Callable和Future | Aoho''s Blog
Spring @Async异步线程池用法总结
C++11 并发指南四(<future> 详解二 std::packaged
C#异步调用(Asynchronou Delegate)
浅析C# 异步编程的两种方式
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服