
org.jvnet.hk2.component.concurrent.WorkManager Maven / Gradle / Ivy
The newest version!
/*
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2007-2010 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common Development
* and Distribution License("CDDL") (collectively, the "License"). You
* may not use this file except in compliance with the License. You can obtain
* a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
* or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
* language governing permissions and limitations under the License.
*
* When distributing the software, include this License Header Notice in each
* file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
* Sun designates this particular file as subject to the "Classpath" exception
* as provided by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the License
* Header, with the fields enclosed by brackets [] replaced by your own
* identifying information: "Portions Copyrighted [year]
* [name of copyright owner]"
*
* Contributor(s):
*
* If you wish your version of this file to be governed by only the CDDL or
* only the GPL Version 2, indicate your decision by adding "[Contributor]
* elects to include this software in this distribution under the [CDDL or GPL
* Version 2] license." If you don't indicate a single choice of license, a
* recipient has the option to distribute your version of this file under
* either the CDDL, the GPL Version 2 or to extend the choice of license to
* its licensees as provided above. However, if you add GPL Version 2 code
* and therefore, elected the GPL Version 2 license, then the option applies
* only if the new code is made subject to such option by the copyright
* holder.
*/
package org.jvnet.hk2.component.concurrent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicInteger;
import org.jvnet.hk2.component.ComponentException;
import com.sun.hk2.component.Hk2ThreadContext;
/**
* Simple helper for managing work sent to a foreign executor service.
*
*
* Has similarities to Fork and Join.
*
*
* The implementation is designed such that Tasks-1 are sent to the executor
* service for possibly another thread to handle. The last task is executed
* by the caller thread so that all threads are attempted to be fully utilized
* for processing including the caller's thread.
*
* @author Jeff Trent
*/
public class WorkManager implements Executor {
private final Executor exec;
private int tasksToDo;
private volatile int count;
private final AtomicInteger workInProgressCount;
private volatile ArrayList errors;
public WorkManager(Executor exec) {
this.exec = exec;
this.workInProgressCount = new AtomicInteger();
}
public WorkManager(Executor exec, int tasksToDo) {
this.exec = exec;
this.workInProgressCount = new AtomicInteger();
this.tasksToDo = tasksToDo;
}
public int getWorkInProgressCount() {
return workInProgressCount.get();
}
public void awaitCompletion() {
synchronized (workInProgressCount) {
if (workInProgressCount.get() > 0) {
try {
workInProgressCount.wait();
} catch (InterruptedException e) {
throw new ExecutionException(e);
}
}
awaitCompletionResults();
}
}
public void awaitCompletion(long timeout, TimeUnit unit) throws TimeoutException {
synchronized (workInProgressCount) {
if (workInProgressCount.get() > 0) {
try {
workInProgressCount.wait(unit.convert(timeout, TimeUnit.MILLISECONDS));
} catch (InterruptedException e) {
throw new ExecutionException(e);
}
if (workInProgressCount.get() > 0) {
throw new TimeoutException();
}
}
awaitCompletionResults();
}
}
private void awaitCompletionResults() {
assert(0 == workInProgressCount.get());
if (null != errors && !errors.isEmpty()) {
ArrayList errors = new ArrayList(this.errors);
this.errors.clear();
throw (1 == errors.size() && ComponentException.class.isInstance(errors.get(0)))
? (ComponentException)errors.get(0) : new ExecutionException(errors);
}
}
protected void completed(Watcher> watcher, Exception e) {
// System.out.print(watcher.toString() + " mark completed on thread " + Thread.currentThread() + "...");
assert(null != watcher);
synchronized (workInProgressCount) {
if (null != e) {
if (null == errors) {
errors = new ArrayList();
}
errors.add(e);
}
int val = workInProgressCount.decrementAndGet();
if (val <= 0) {
workInProgressCount.notifyAll();
}
// System.out.println("done: " + val);
}
}
@SuppressWarnings("unchecked")
public Collection> submitAll(Collection> tasks) {
workInProgressCount.addAndGet(tasks.size());
ArrayList> futures = new ArrayList>();
for (Callable> task : tasks) {
assert(null != task);
Watcher watcherTask = new Watcher(task);
futures.add(watcherTask);
if (++count == tasksToDo) {
watcherTask.runNow();
} else {
exec.execute(watcherTask);
}
}
return futures;
}
@SuppressWarnings("unchecked")
public void executeAll(Collection tasks) {
workInProgressCount.addAndGet(tasks.size());
for (Runnable task : tasks) {
assert(null != task);
Watcher watcherTask = new Watcher(task, null);
if (++count == tasksToDo) {
watcherTask.runNow();
} else {
exec.execute(watcherTask);
}
}
}
@SuppressWarnings({ "unused", "unchecked" })
public Future submit(Callable task) {
assert(null != task);
int work = workInProgressCount.incrementAndGet();
// System.out.print("adding more: " + task + "; count=" + work);
Watcher watcherTask = new Watcher(task);
if (++count == tasksToDo) {
watcherTask.runNow();
} else {
exec.execute(watcherTask);
}
return watcherTask;
}
@SuppressWarnings("unchecked")
@Override
public void execute(Runnable task) {
assert(null != task);
workInProgressCount.incrementAndGet();
Watcher watcherTask = new Watcher(task, null);
if (++count == tasksToDo) {
watcherTask.runNow();
} else {
exec.execute(watcherTask);
}
}
private final class Watcher extends FutureTask {
public Watcher(Callable task) {
super(task);
}
public Watcher(Runnable runnable, V result) {
super(runnable, result);
}
@Override
public void run() {
Runnable runnable = new Runnable() {
@Override
public void run() {
runNow();
}
};
Hk2ThreadContext.captureACCandRun(runnable);
}
private void runNow() {
try {
super.run();
completed(this, null);
} catch (Exception e){
setException(e);
completed(this, e);
}
}
}
@SuppressWarnings("serial")
public static final class ExecutionException extends ComponentException {
protected List extends Throwable> cause;
public ExecutionException(Throwable t) {
super(t.getMessage(), t);
this.cause = Collections.singletonList(t);
}
public ExecutionException(ExecutionException e) {
super(e.getMessage(), e.getCause());
this.cause = Collections.singletonList(e);
}
public ExecutionException(List errors) {
super(errors.get(0).getMessage(), errors.get(0));
this.cause = errors;
}
public T getCause(Class eClass) {
for (Throwable e : cause) {
if (null != e.getCause() && eClass == e.getCause().getClass()) {
return eClass.cast(e.getCause());
}
}
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy