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

com.nflabs.zeppelin.scheduler.FIFOScheduler Maven / Gradle / Ivy

There is a newer version: 0.3.3
Show newest version
package com.nflabs.zeppelin.scheduler;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutorService;

public class FIFOScheduler implements Scheduler{
	List queue = new LinkedList();
	private ExecutorService executor;
	private SchedulerListener listener;
	boolean terminate=false;
	Job runningJob = null;
	private String name;

	public FIFOScheduler(String name, ExecutorService executor, SchedulerListener listener){
		this.name = name;
		this.executor = executor;
		this.listener = listener;
	}
	
	public String getName(){
		return name;
	}
	public Collection getJobsWaiting(){
		List ret = new LinkedList();
		synchronized(queue){
			for(Job job : queue){
				ret.add(job);
			}
		}
		return ret;
	}
	
	public Collection getJobsRunning(){
		List ret = new LinkedList();
		Job job = runningJob;
		
		if(job!=null)
			ret.add(job);
			
		return ret;
	}
	
	
	
	public void submit(Job job){
		synchronized(queue){
			queue.add(job);
			queue.notify();
		}
	}
	
	public void run(){

		synchronized(queue){		
			while(terminate==false){			
				if(runningJob!=null || queue.isEmpty()==true){
					try {
						queue.wait(500);
					} catch (InterruptedException e) {
					}
					continue;
				}

				runningJob = queue.remove(0);
					
				final Scheduler scheduler = this;
				this.executor.execute(new Runnable(){
					public void run() {
						if(listener!=null) listener.jobStarted(scheduler, runningJob);
						runningJob.run();
						if(listener!=null) listener.jobFinished(scheduler, runningJob);
						runningJob = null;
						synchronized(queue){
							queue.notify();
						}
					}					
				});
			}
				

		}
	}
	
	public void stop(){
		terminate = true;
		synchronized(queue){
			queue.notify();
		}
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy