package ws.prova;

/*
 * Copyright (C) 2002-2007 <a href="mailto:a.kozlenkov@city.ac.uk">Alex Kozlenkov</a>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.Vector;

import edu.emory.mathcs.backport.java.util.concurrent.ExecutorService;
import edu.emory.mathcs.backport.java.util.concurrent.Executors;

/**
 * TaskQueue class for processing Task(s)
 * <p>Title: Prova</p>
 * <p>Description: TaskQueue class for processing Task(s)</p>
 * <p>Copyright: Copyright (C) 2002-2007</p>
 * <p>Company: City University, London, United Kingdom </p>
 *
 * @author Alex Kozlenkov
 * @version 1.9.0 <19 November 2005>
 */

public class TaskQueue implements ITaskQueue {
//	private Vector tasks = new Vector();
//	private boolean waiting;
//	private boolean shutdown;
	private static final int poolSize = 16;
	private final ExecutorService pool;

	public void setShutdown() {
		pool.shutdown();
	}

	public TaskQueue() {
//		tasks = new Vector();
//		waiting = false;
		pool = Executors.newFixedThreadPool(poolSize);
//		new Thread(new Worker()).start();
	}

	public TaskQueue( int poolSize ) {
//		tasks = new Vector();
//		waiting = false;
		pool = Executors.newFixedThreadPool(poolSize);
//		new Thread(new Worker()).start();
	}

	public void put( final ITask r ) {
		pool.submit(
				new Runnable() {
					public void run() {
						if( r!=null )
							r.execute();
					}
				}
		);
//		tasks.add(r);
//		if (waiting) {
//			synchronized (this) {
//				if( !tasks.isEmpty() )
//					notifyAll();
//			}
//		}
	}

//	public ITask take() {
//		if (tasks.isEmpty()) {
//			synchronized (this) {
//				waiting = true;
//				try {
//					wait();
//				} catch (InterruptedException ie) {
//					waiting = false;
//				}
//				if (tasks.isEmpty()) {
//					// Reawaken by shutdown
//					return null;
//				}
//			}
//		}
//		return (ITask) tasks.remove(0);
//	}
//
//	private class Worker implements Runnable {
//		public void run() {
//			while (!shutdown) {
//				ITask r = take();
//				if (r != null) {
//					r.execute();
//				}
//			}
//		}
//	}
}
