#525 加强Utils Modules模块:Concurrent部分完成小目标
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* ==================================================================== Licensed to the Apache Software Foundation (ASF)
|
||||
* under one or more contributor license agreements. See the NOTICE file distributed with this work for additional
|
||||
* information regarding copyright ownership. The ASF licenses this file to you 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.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many individuals on behalf of the Apache Software
|
||||
* Foundation. For more information on the Apache Software Foundation, please see <http://www.apache.org/>.
|
||||
*
|
||||
*/
|
||||
package org.springside.modules.utils.concurrent;
|
||||
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import org.apache.commons.lang3.Validate;
|
||||
|
||||
/**
|
||||
* 从Apache HttpClient 4.2 移植,一个Future实现类的基本框架.
|
||||
*/
|
||||
public abstract class BasicFuture<T> implements Future<T> {
|
||||
|
||||
protected volatile boolean completed;
|
||||
private volatile boolean cancelled;
|
||||
private volatile T result;
|
||||
private volatile Exception ex;
|
||||
|
||||
@Override
|
||||
public boolean isCancelled() {
|
||||
return this.cancelled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isDone() {
|
||||
return this.completed;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized T get() throws InterruptedException, ExecutionException {
|
||||
while (!this.completed) {
|
||||
wait();
|
||||
}
|
||||
return getResult();
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized T get(final long timeout, final TimeUnit unit)
|
||||
throws InterruptedException, ExecutionException, TimeoutException {
|
||||
Validate.notNull(unit, "Time unit");
|
||||
final long msecs = unit.toMillis(timeout);
|
||||
final long startTime = (msecs <= 0) ? 0 : System.currentTimeMillis();
|
||||
long waitTime = msecs;
|
||||
if (this.completed) {
|
||||
return getResult();
|
||||
} else if (waitTime <= 0) {
|
||||
throw new TimeoutException();
|
||||
} else {
|
||||
for (;;) {
|
||||
wait(waitTime);
|
||||
if (this.completed) {
|
||||
return getResult();
|
||||
} else {
|
||||
waitTime = msecs - (System.currentTimeMillis() - startTime);
|
||||
if (waitTime <= 0) {
|
||||
throw new TimeoutException();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private T getResult() throws ExecutionException {
|
||||
if (this.ex != null) {
|
||||
throw new ExecutionException(this.ex);
|
||||
}
|
||||
return this.result;
|
||||
}
|
||||
|
||||
public boolean completed(final T result) {
|
||||
synchronized (this) {
|
||||
if (this.completed) {
|
||||
return false;
|
||||
}
|
||||
this.completed = true;
|
||||
this.result = result;
|
||||
notifyAll();
|
||||
}
|
||||
onCompleted(result);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public boolean failed(final Exception exception) {
|
||||
synchronized (this) {
|
||||
if (this.completed) {
|
||||
return false;
|
||||
}
|
||||
this.completed = true;
|
||||
this.ex = exception;
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
onFailed(exception);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean cancel(final boolean mayInterruptIfRunning) {
|
||||
synchronized (this) {
|
||||
if (this.completed) {
|
||||
return false;
|
||||
}
|
||||
this.completed = true;
|
||||
this.cancelled = true;
|
||||
notifyAll();
|
||||
}
|
||||
|
||||
onCancelled();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected abstract void onCompleted(T result);
|
||||
|
||||
protected abstract void onFailed(Exception ex);
|
||||
|
||||
protected abstract void onCancelled();
|
||||
}
|
||||
+8
-1
@@ -16,7 +16,6 @@ import org.slf4j.LoggerFactory;
|
||||
* 因为ThreadInfo的toString()最多只会打印8层的StackTrace,所以加上了最大打印层数的选项.(默认为8)
|
||||
*
|
||||
* @author calvin
|
||||
*
|
||||
*/
|
||||
public class ThreadDumpper {
|
||||
|
||||
@@ -31,6 +30,14 @@ public class ThreadDumpper {
|
||||
private ThreadMXBean threadMBean = ManagementFactory.getThreadMXBean();
|
||||
private volatile Long lastThreadDumpTime = 0L;
|
||||
|
||||
public ThreadDumpper(){
|
||||
}
|
||||
|
||||
public ThreadDumpper(long leastIntervalMills, int maxStackLevel) {
|
||||
this.leastIntervalMills = leastIntervalMills;
|
||||
this.maxStackLevel = maxStackLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
* 符合条件则打印线程栈.
|
||||
*/
|
||||
|
||||
-1
@@ -23,7 +23,6 @@ import com.google.common.annotations.Beta;
|
||||
* };
|
||||
* </pre>
|
||||
*/
|
||||
@Beta
|
||||
public class ThreadLocalContext {
|
||||
|
||||
private static ThreadLocal<Map<String, Object>> contextMap = new ThreadLocal<Map<String, Object>>() {
|
||||
|
||||
@@ -17,6 +17,14 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
|
||||
/**
|
||||
* 线程相关工具类.
|
||||
*
|
||||
* 1. 处理了InterruptedException的sleep()
|
||||
*
|
||||
* 2. 优雅关闭线程池的gracefulShutdown()
|
||||
*
|
||||
* 3. 创建可自定义线程名的ThreadFactory的buildThreadFactory()
|
||||
*
|
||||
* 4. 防止第三方Runnalbe未捕捉异常导致线程跑飞的wrapExceptionRunnable()
|
||||
*/
|
||||
public class Threads {
|
||||
|
||||
|
||||
-1
@@ -6,7 +6,6 @@ import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
*
|
||||
* from Guava 20.0
|
||||
*/
|
||||
final class LongAdder extends Striped64 implements Serializable {
|
||||
|
||||
+35
-3
@@ -19,7 +19,11 @@ import org.springside.modules.utils.concurrent.threadpool.QueableCachedThreadPoo
|
||||
/**
|
||||
* ThreadPool创建的工具类.
|
||||
*
|
||||
* 对比JDK Executors中的newFixedThreadPool(), newCachedThreadPool(),newScheduledThreadPool, 提供更多有用的配置项. 使用示例:
|
||||
* 对比JDK Executors中的newFixedThreadPool(), newCachedThreadPool(),newScheduledThreadPool, 提供更多有用的配置项.
|
||||
*
|
||||
* 另包含了移植自Tomcat的QueuableCachedPool.
|
||||
*
|
||||
* 使用示例如下:
|
||||
*
|
||||
* <pre>
|
||||
* ExecutorService ExecutorService = new FixedThreadPoolBuilder().setPoolSize(10).build();
|
||||
@@ -31,6 +35,34 @@ public class ThreadPoolBuilder {
|
||||
|
||||
private static RejectedExecutionHandler defaultRejectHandler = new AbortPolicy();
|
||||
|
||||
/**
|
||||
* @see FixedThreadPoolBuilder
|
||||
*/
|
||||
public static FixedThreadPoolBuilder fixedPool() {
|
||||
return new FixedThreadPoolBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see CacheedThreadPoolBuilder
|
||||
*/
|
||||
public static CachedThreadPoolBuilder cachedPool() {
|
||||
return new CachedThreadPoolBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see ScheduledThreadPoolBuilder
|
||||
*/
|
||||
public static ScheduledThreadPoolBuilder scheduledPool() {
|
||||
return new ScheduledThreadPoolBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* @see QueableCachedThreadPoolBuilder
|
||||
*/
|
||||
public static QueableCachedThreadPoolBuilder queableCachedPool() {
|
||||
return new QueableCachedThreadPoolBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建FixedThreadPool.
|
||||
*
|
||||
@@ -227,7 +259,7 @@ public class ThreadPoolBuilder {
|
||||
return new ScheduledThreadPoolExecutor(poolSize, threadFactory);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从Tomcat移植过来的可扩展可用Queue缓存任务的ThreadPool
|
||||
*
|
||||
@@ -255,7 +287,7 @@ public class ThreadPoolBuilder {
|
||||
this.maxSize = maxSize;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueableCachedThreadPoolBuilder setQueueSize(int queueSize) {
|
||||
this.queueSize = queueSize;
|
||||
return this;
|
||||
|
||||
Reference in New Issue
Block a user