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

org.appdapter.gui.browse.SpecialQueue Maven / Gradle / Ivy

Go to download

Appdapter Maven project including Java and Scala, produces jar, not bundle. Excludes concrete SLF4J binding.

The newest version!
package org.appdapter.gui.browse;

import java.lang.Thread.UncaughtExceptionHandler;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;

import org.appdapter.core.convert.ReflectUtils;
import org.appdapter.core.log.BasicDebugger;

import com.hp.hpl.jena.rdf.model.Model;

/**
 * Repo loading in parallel
 * 
 * LogicMoo
 */
public class SpecialQueue extends BasicDebugger implements UncaughtExceptionHandler {
	public enum SheetLoadStatus {
		Pending, Loading, Loaded, Unloading, Unloaded, Cancelling, Cancelled, Error
	}

	public void logWarning(String msg) {
		getLogger().warn(msg);
	}

	ExecutorService executor = null;
	LinkedList tasks = new LinkedList();
	boolean lastJobSubmitted = false;
	Object synchronousAdderLock = new Object();
	boolean isSynchronous = true;
	int taskNum = 0;
	Object loaderFor = null;
	String repoStr = "Utility Special Queue";
	// sounds like a lot.. but it is over with quickly!
	int numThreads = 32;
	int howManyTasksBeforeStartingPool = 0;

	public SpecialQueue(Object repo) {
		loaderFor = repo;
	}

	public void setSynchronous(boolean isSync) {
		isSynchronous = isSync;
		if (isSync) {
			synchronized (synchronousAdderLock) {
				waitUntilLastJobComplete();
				if (executor != null) {
					executor.shutdown();
					executor = null;
				}
			}
		}
	}

	public void reset() {
		synchronized (synchronousAdderLock) {
			lastJobSubmitted = false;
		}
	}

	public void setLastJobSubmitted() {
		synchronized (synchronousAdderLock) {
			lastJobSubmitted = true;
		}
	}

	@Override
	public String toString() {
		if (false) {
			StringBuilder sbuf = new StringBuilder();
			int num = tasksWithsStatus(sbuf, true, SheetLoadStatus.Loaded);
			if (num > 0)
				return sbuf.toString();
		}
		return super.toString();
	}

	int tasksWithsStatus(StringBuilder sb, boolean neg, SheetLoadStatus... statuses) {
		int taskNum = 0;
		for (SheetLoadStatus status : statuses) {
			for (Task task : ReflectUtils.copyOf(tasks)) {
				if (task.getLoadStatus() != status) {
					if (!neg)
						continue;
				} else {
					if (neg)
						continue;
				}
				taskNum++;
				sb.append("" + taskNum + ": " + task.toString() + "\n");
			}
		}
		return taskNum;
	}

	/**
	 * Wait for the last load to happens
	 */
	public void waitUntilLastJobComplete() {

		int origTaskSize = 0;
		int newTaskSize = tasks.size();
		while (origTaskSize != newTaskSize) {
			for (Task task : ReflectUtils.copyOf(tasks)) {
				Task sheetLoadResult = task.get();
			}
			origTaskSize = newTaskSize;
			newTaskSize = tasks.size();
		}
		boolean waslastJobSubmitted;
		synchronized (synchronousAdderLock) {
			waslastJobSubmitted = lastJobSubmitted;
		}
		if (waslastJobSubmitted) {
			if (executor == null)
				return;
			//logWarning("Shutting down executor for " + repoStr);
			// executor.shutdown();
			// executor = null;
		} else {
			///logError("To Early to have called waitUntilLastJobComplete");
		}
	}
/*
	public void addTask(Runnable r) {
		addTask("" + r, r);
	}
*/
	public void addTask(String named0, Runnable r) {
		Task task = new Task(named0, r);
		//synchronized (synchronousAdderLock) 
		{
			if (isSynchronous || taskNum < howManyTasksBeforeStartingPool) {
				taskNum++;
				task.call();
				return;
			}

			if (executor == null) {
				lastJobSubmitted = false;
				logWarning("Creating executor for " + repoStr);
				executor = Executors.newFixedThreadPool(numThreads, new ThreadFactory() {
					@Override
					public Thread newThread(final Runnable r) {
						return new Thread("Worker " + ++workrNum + " for " + loaderFor) {
							public void run() {
								r.run();
							}

							@Override
							public UncaughtExceptionHandler getUncaughtExceptionHandler() {
								return SpecialQueue.this;
							}
						};

					}

				});
			}
			synchronized (tasks) {
				tasks.add(task);
			}
			task.future = (Future) executor.submit((Runnable) task);
		}
	}

	public int totalTasks = 0;
	public int workrNum = 0;

	/** Try to sheetLoad a URL. Return true only if successful. */
	public final class Task implements Callable, Runnable {
		final String taskName;
		final int taskNum = totalTasks++;
		SheetLoadStatus sheetLoadStatus = SheetLoadStatus.Unloaded;
		Future future;
		Runnable runIt;
		long start = -1, end = -1;
		Throwable lastException;

		@Override
		public String toString() {
			long soFar = (end == -1) ? System.currentTimeMillis() - start : end - start;
			return "TASK: " + taskName + " status=" + getLoadStatus() + " msecs=" + soFar + (lastException == null ? "" : " error=" + lastException);
		}

		public Task(String named0, Runnable r) {
			this.taskName = named0;
			runIt = r;
			postLoadStatus(SheetLoadStatus.Pending, false);
		}

		void error(Throwable t) {
			lastException = t;
			logError(toString(), t);
		}

		@Override
		public void run() {
			call();
		}

		public Task get() {
			try {
				if (end != -1)
					return this;
				return future.get();
			} catch (Throwable e) {
				error(e);
				postLoadStatus(SheetLoadStatus.Error, true);
			}
			return this;
		}

		public Task call() {
			postLoadStatus(SheetLoadStatus.Loading, false);
			try {
				if (end != -1)
					return this;
				runIt.run();
				postLoadStatus(SheetLoadStatus.Loaded, true);
			} catch (Throwable e) {
				error(e);
				postLoadStatus(SheetLoadStatus.Error, true);
			}
			return this;
		}

		SheetLoadStatus getLoadStatus() {
			return sheetLoadStatus;
		}

		void postLoadStatus(SheetLoadStatus newLoadStatus, boolean isEnd) {
			if (newLoadStatus == this.sheetLoadStatus)
				return;
			long curMS = System.currentTimeMillis();
			if (isEnd) {
				this.end = curMS;
			} else {
				this.start = curMS;
			}
			this.sheetLoadStatus = newLoadStatus;
			String info = toString();
			Thread ct = Thread.currentThread();
			if (ct.getUncaughtExceptionHandler() instanceof SpecialQueue) {
				ct.setName(info);
			}
			logInfo(info);
		}
	}

	@Override
	public void uncaughtException(Thread t, Throwable e) {
		logError(" uncaughtException on " + t, e);
		e.printStackTrace();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy