Spring 擅长对组件的封装和集成, Spring-context对JDK的并发包做了功能增强。
<bean id="asyncTaskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
<property name="corePoolSize" value="10" />
<property name="maxPoolSize" value="10" />
<property name="keepAliveSeconds" value="60" />
<property name="queueCapacity" value="100" />
<property name="allowCoreThreadTimeOut" value="false" />
</bean>
以上asyncTaskExecutor,你可以注入到任何一个bean去执行,底层使用JDK的ThreadPoolTaskExecutor来管理线程,默认使用的是JDK的线程池.
@Autowired
@Qualifier("asyncTaskExecutor")
private ThreadPoolTaskExecutor asyncTaskExecutor;
FutureTask<String> f1 = (FutureTask<String>)asyncTaskExecutor.submit(c1);
if (f1.isDone()) {
String result = future.get();
}
以上只是简单的应用,非常方便的开发,我们都不用去处理线程池的初始化,以及线程的管理。
Spring 还增加了对线程的监听,这样当现场成功的时候做一些处理,线程失败的时候做一些处理。
ListenableFutureTask<String> f1 = (ListenableFutureTask<String>) asyncTaskExecutor.submitListenable(c1);
f1.addCallback(new ListenableFutureCallback<String>() {
@Override
public void onSuccess(String result) {
//TODO
}
@Override
public void onFailure(Throwable t) {
// TODO Auto-generated method stub
t.printStackTrace();
}
});
这里Spring非常机智的扩展FutureTask的protected方法
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util.concurrent;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* Extension of {@link FutureTask} that implements {@link ListenableFuture}.
*
* @author Arjen Poutsma
* @since 4.0
*/
public class ListenableFutureTask<T> extends FutureTask<T> implements ListenableFuture<T> {
private final ListenableFutureCallbackRegistry<T> callbacks = new ListenableFutureCallbackRegistry<T>();
/**
* Create a new {@code ListenableFutureTask} that will, upon running,
* execute the given {@link Callable}.
* @param callable the callable task
*/
public ListenableFutureTask(Callable<T> callable) {
super(callable);
}
/**
* Create a {@code ListenableFutureTask} that will, upon running,
* execute the given {@link Runnable}, and arrange that {@link #get()}
* will return the given result on successful completion.
* @param runnable the runnable task
* @param result the result to return on successful completion
*/
public ListenableFutureTask(Runnable runnable, T result) {
super(runnable, result);
}
@Override
public void addCallback(ListenableFutureCallback<? super T> callback) {
this.callbacks.addCallback(callback);
}
@Override
protected final void done() {
Throwable cause;
try {
T result = get();
this.callbacks.success(result);
return;
}
catch (InterruptedException ex) {
Thread.currentThread().interrupt();
return;
}
catch (ExecutionException ex) {
cause = ex.getCause();
if (cause == null) {
cause = ex;
}
}
catch (Throwable ex) {
cause = ex;
}
this.callbacks.failure(cause);
}
}
ListenableFutureCallbackRegistry做了一个简单的策略, 可以添加CallBack, 有两方法success 和 failure.
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.util.concurrent;
import java.util.LinkedList;
import java.util.Queue;
import org.springframework.util.Assert;
/**
* Registry for {@link ListenableFutureCallback} instances.
*
* <p>Inspired by {@code com.google.common.util.concurrent.ExecutionList}.
*
* @author Arjen Poutsma
* @since 4.0
*/
public class ListenableFutureCallbackRegistry<T> {
private final Queue<ListenableFutureCallback<? super T>> callbacks =
new LinkedList<ListenableFutureCallback<? super T>>();
private State state = State.NEW;
private Object result = null;
private final Object mutex = new Object();
/**
* Adds the given callback to this registry.
* @param callback the callback to add
*/
@SuppressWarnings("unchecked")
public void addCallback(ListenableFutureCallback<? super T> callback) {
Assert.notNull(callback, "'callback' must not be null");
synchronized (mutex) {
switch (state) {
case NEW:
callbacks.add(callback);
break;
case SUCCESS:
callback.onSuccess((T)result);
break;
case FAILURE:
callback.onFailure((Throwable) result);
break;
}
}
}
/**
* Triggers a {@link ListenableFutureCallback#onSuccess(Object)} call on all added
* callbacks with the given result
* @param result the result to trigger the callbacks with
*/
public void success(T result) {
synchronized (mutex) {
state = State.SUCCESS;
this.result = result;
while (!callbacks.isEmpty()) {
callbacks.poll().onSuccess(result);
}
}
}
/**
* Triggers a {@link ListenableFutureCallback#onFailure(Throwable)} call on all added
* callbacks with the given {@code Throwable}.
* @param t the exception to trigger the callbacks with
*/
public void failure(Throwable t) {
synchronized (mutex) {
state = State.FAILURE;
this.result = t;
while (!callbacks.isEmpty()) {
callbacks.poll().onFailure(t);
}
}
}
private enum State {NEW, SUCCESS, FAILURE}
}
这里使用LinkedQueue 来维护Callbacks. 使用add()添加,使用pool()方法来获取Queue的first head去执行success/failure,然后删除,使用LinkedQueue对于
经常insert/delete的队列可是非常快的,很多东西可以学习的(Spring source code).
其实我是不大喜欢自己去实现一套,Spring封装的挺好,跑题了,呵呵.
ListenableFutureTask 重写了JDK FutureTask.done().
/**
* Protected method invoked when this task transitions to state
* {@code isDone} (whether normally or via cancellation). The
* default implementation does nothing. Subclasses may override
* this method to invoke completion callbacks or perform
* bookkeeping. Note that you can query status inside the
* implementation of this method to determine whether this task
* has been cancelled.
*/
protected void done() { }
当任务结束的时候,FutureTask会调用这个方法。所以这个方法还能异步调用。
比如
task1 execute 10s
task2 execute 20s
在同时执行的情况下,task1's callback 方法会先返回, 在实际中,如果有10个任务,这个功能先返回执行完成的任务给客户,对于实时性要求比较高的应用,这是个不错的解决方案。