Skip to content

Instantly share code, notes, and snippets.

@dulichan
Created March 24, 2013 18:02
Show Gist options
  • Save dulichan/5232859 to your computer and use it in GitHub Desktop.
Save dulichan/5232859 to your computer and use it in GitHub Desktop.
Asynchronous task executer that uses an ongoing thread to monitor the tasks.
package org.dchan.concurrent;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
/**
* Copyright (c) 2013 Dulitha Wijewantha (http://www.dulithawijewantha.com)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This class facilitates the execution of Asynchronous method calls. For
* example where their is no feedback necessary. An instance of this class is
* considered as a single Cue. The cue uses a single thread (like a service) to
* execute tasks). {@link Runnable} instances are used to run the tasks.
*
* @author Chan (Dulitha R. Wijewantha [email protected])
* @version 1.1
*
*/
public class AsyncExecuter {
/**
* Consists of the tasks
*/
private Queue<Runnable> taskList = new LinkedList<Runnable>();
// Switch to stop the service thread
private boolean stop = false;
/**
* At instantiation the service thread is started.
*/
public AsyncExecuter() {
Thread thread = new Thread(new Runnable() {
public void run() {
// Will run until stop variable becomes true
while (!stop) {
startTaskEngine();
}
}
});
thread.start();
}
private void startTaskEngine() {
if (!taskList.isEmpty()) {
Runnable type = (Runnable) taskList.poll();
type.run();
try {
Thread.sleep(100);
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Add a task for execution
*
* @param runnable
*/
public void addTask(Runnable runnable) {
taskList.add(runnable);
}
/**
* Stop the service thread.
*/
public void stop() {
stop = true;
}
/**
*
* @return true if the service is not active and has tasks that weren't
* executed
*/
public boolean isDead() {
if (!isActive()) {
if (taskList.size() > 0) {
return true;
}
}
return false;
}
/**
*
* @return true if active. False if dead.
*/
public boolean isActive() {
return !stop;
}
/**
* Creates a clone {@link AsyncExecuter} which contains tasks that were not
* executed previously
*/
public AsyncExecuter clone() {
AsyncExecuter asyncExecuter = new AsyncExecuter();
for (Iterator iterator = taskList.iterator(); iterator.hasNext();) {
Runnable type = (Runnable) iterator.next();
asyncExecuter.addTask(type);
}
return asyncExecuter;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment