Javascript 思考角度';s$q.all使用Java并发
在Angular.js库中,我们有一个api$q $q.all()接受承诺数组,即Javascript 思考角度';s$q.all使用Java并发,javascript,java,angularjs,concurrency,angular-promise,Javascript,Java,Angularjs,Concurrency,Angular Promise,在Angular.js库中,我们有一个api$q $q.all()接受承诺数组,即 $q.all([promise1,promise2,....,promiseN]).then(function); 只有在解析传入参数的所有承诺后,才会解析回调函数 正如您在下面的代码中所看到的 <script type="text/javaScript"> angular.module("myApp",[]); angular.module("myA
$q.all([promise1,promise2,....,promiseN]).then(function);
只有在解析传入参数的所有承诺后,才会解析回调函数
正如您在下面的代码中所看到的
<script type="text/javaScript">
angular.module("myApp",[]);
angular.module("myApp").controller("myCtrl",myCtrl);
angular.module("myApp").factory("demoService",demoService);
myCtrl.$inject = ["$scope","demoService","$q"];
demoService.$inject = ["$q","$timeout"];
function myCtrl($scope,demoService,$q){
$scope.message = "All promises";
$scope.init = init;
function invokedAll(){
var promises = [demoService.get(1),
demoService.get(2),
demoService.get(3),
demoService.get(4),
demoService.get(5),
demoService.get(6),
demoService.get(7),
demoService.get(8),
demoService.get(9),
demoService.get(10)];
return $q.all(promises);
} //invokedAll
function init(){
invokedAll().then(function(){
console.log(" Data loaded successfully ");
});
}
}
function demoService($q,$timeout){
var TIMEOUT = 2000; // 5 seconds
return {
get : function(messageNo){
TIMEOUT += 1000;
var deferred = $q.defer();
$timeout(function(){
console.log("data loaded "+messageNo);
deferred.resolve("success");
},TIMEOUT);
return deferred.promise;
}
};
}
</script>
<body ng-controller="myCtrl" ng-init="init()">
您可以看到,只有在所有承诺都得到解决之后,才会打印最后一行
我试图用java实现的上述功能
我正在使用ExecuterService api在线程池中执行任务
所有任务执行完毕后,调用final
代码如下:
package executerService.example01;
public class Example05 {
private static final int POOL_COUNT = 10;
private static void tasks() throws Exception{
ExecutorService service = null;
Set<Callable<Integer>> callables = null;
List<Future<Integer>> futures = null;
service = Executors.newFixedThreadPool(POOL_COUNT);
callables = new HashSet<Callable<Integer>>();
callables.add(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int taskNo = 1;
System.out.println(Thread.currentThread().getName()+", TASK-NO "+taskNo);
return taskNo;
}
});
callables.add(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int taskNo = 2;
System.out.println(Thread.currentThread().getName()+", TASK-NO "+taskNo);
return taskNo;
}
});
callables.add(new Callable<Integer>() {
@Override
public Integer call() throws Exception {
int taskNo = 3;
System.out.println(Thread.currentThread().getName()+", TASK-NO "+taskNo);
return taskNo;
}
});
futures = service.invokeAll(callables);
for(Iterator<Future<Integer>> itr = futures.iterator(); itr.hasNext();){
System.out.println("Execution of Task No "+itr.next().get()+" completed");
}
} //end of method task
public static void main(String [] args){
ExecutorService service = null;
Future<Boolean> future = null;
try{
service = Executors.newSingleThreadExecutor();
future = service.submit(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
tasks();
return true;
}
});
if(future.get()){
System.out.println("All the tasks are executed");
}
}catch(Exception e){
e.printStackTrace();
}
} // end of main method
}
pool-2-thread-1, TASK-NO 2
pool-2-thread-2, TASK-NO 1
pool-2-thread-3, TASK-NO 3
Execution of Task No 2 completed
Execution of Task No 1 completed
Execution of Task No 3 completed
All the tasks are executed
我的问题是
我是否能够实现angular.js的“$q.all()”的相同行为在Java中使用“ExecuterService.invokeAll()”和“Future.get()” 如果我的理解是书面的,请指导我
还请让我知道,上面的Java实现是否正确,或者可以用不同的方式实现 什么行为?输出是一样的,不是吗?还请记住,js是一个线程,承诺可能看起来像java线程,但实际上它是完全不同的。它们甚至提供了一个allOf方法,可以让您非常接近JS的行为,但正如Petr所说,总会有一些语义差异
pool-2-thread-1, TASK-NO 2
pool-2-thread-2, TASK-NO 1
pool-2-thread-3, TASK-NO 3
Execution of Task No 2 completed
Execution of Task No 1 completed
Execution of Task No 3 completed
All the tasks are executed