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

com.emc.mongoose.common.concurrent.ConcurrentQueueTaskSequencer Maven / Gradle / Ivy

The newest version!
package com.emc.mongoose.common.concurrent;

import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Future;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.atomic.AtomicInteger;

/**
 Created by kurila on 13.07.16.
 */
@Deprecated
public final class ConcurrentQueueTaskSequencer
extends Thread
implements TaskSequencer {

	@Deprecated
	public static final ConcurrentQueueTaskSequencer INSTANCE = new ConcurrentQueueTaskSequencer(
		"concurrentQueueTaskSequencer", true
	);

	private final Queue queue;
	private final AtomicInteger queueSize = new AtomicInteger(0);

	private ConcurrentQueueTaskSequencer(final String name, boolean daemonFlag) {
		super(name);
		setDaemon(daemonFlag);
		queue = new ConcurrentLinkedQueue<>();
	}

	@Deprecated @Override
	public final  Future submit(final RunnableFuture task) {
		while(true) {
			if(queueSize.get() < DEFAULT_TASK_QUEUE_SIZE_LIMIT) {
				if(queue.add(task)) {
					return task;
				}
			}
			Thread.yield();
		}
	}

	@Override
	public final void run() {
		Runnable nextTask;
		try {
			while(!isInterrupted()) {
				nextTask = queue.poll();
				if(nextTask != null) {
					try {
						nextTask.run();
					} catch(final Exception e) {
						e.printStackTrace(System.err);
					}
				} else {
					Thread.yield();
				}
			}
		} finally {
			queue.clear();
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy