All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.intarsys.tools.concurrent.TaskSequence Maven / Gradle / Ivy

There is a newer version: 4.11
Show newest version
/*
 * intarsys consulting GmbH
 * all rights reserved
 *
 */
package de.intarsys.tools.concurrent;

import java.util.ArrayList;
import java.util.List;

/**
 * experimental
 */
public class TaskSequence extends AbstractFutureTask {

	private List tasks = new ArrayList();

	private String labelPrefix;

	private Runnable currentTask = null;

	public TaskSequence(String labelPrefix) {
		super();
		// setWorkTotal(0);
		this.labelPrefix = labelPrefix;
	}

	public void addTask(Runnable task, int percent) {
		tasks.add(new TaskStep(this, task, percent));
		// setWorkTotal(getWorkTotal() + percent);
	}

	@Override
	protected R compute() throws Exception {
		try {
			R result = null;
			for (Runnable task : tasks) {
				synchronized (this) {
					currentTask = task;
				}
				task.run();
				if (isCancelled()) {
					return null;
				}
			}
			return result;
		} finally {
			synchronized (this) {
				currentTask = null;
			}
		}
	}

	protected String getLabelPrefix() {
		return labelPrefix;
	}

	protected String getLabelSuffix() {
		Runnable tempTask;
		synchronized (this) {
			tempTask = currentTask;
		}
		return "";
	}

	public void setLabelPrefix(String labelPrefix) {
		this.labelPrefix = labelPrefix;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy