📄 taskqueue.java
字号:
package de.spieleck.util;
import java.util.LinkedList;
/**
* A queue of Tasks ({@link java.lang.Runnable Runnable}s)
* for the {@link ThreadPool}.
*
* @author fsn
* @version 0.0
*/
public class TaskQueue
implements TaskSource
{
/** The queue of Files which need to be processed */
protected LinkedList tasks;
/** Did we allready have an assignement? */
protected boolean started = false;
/** Can we finish now? */
protected boolean workExpected = false;
/** Lock for counting */
protected Object cLock = new Object();
/**
* Construct a queue.
*/
public TaskQueue()
{
tasks = new LinkedList();
}
/**
* Get the next task to execute
*/
public Runnable getTask()
throws InterruptedException
{
synchronized(tasks)
{
while ( tasks.isEmpty() )
{
tasks.wait();
}
Runnable r = (Runnable) tasks.removeFirst();
// In case of fast adding task, this can help:
if ( !tasks.isEmpty() )
tasks.notify();
return r;
}
}
/**
* Add a task to execute
*/
public void addTask(Runnable r)
{
synchronized(tasks)
{
tasks.add(r);
if ( !started )
{
started = true;
tasks.notifyAll();
}
else
tasks.notify();
}
}
/**
* In case we expect that the queue empties in between
* (since we are feeding in tasks slowly and the tasks
* finish fast), but we want to avoid {@link #isWorkLeft}
* to return * the "wrong message", we can use this.
* <pre>
* TaskQueue tq = new TaskQueue(...)
* tq.setWorkExpected(true);
* ....
* </pre>
* But one must not fail to call
* <pre>
* tq.setWorkExpected(false);
* </pre>
* when no feeds are expected any more, since, otherwise
* {@link #isWorkLeft} will return true forever!
*/
public void setWorkExpected(boolean workExpected)
{
synchronized(tasks)
{
this.workExpected = workExpected;
}
}
/**
* Have we been started ?
*/
public boolean isStarted()
{
synchronized(tasks)
{
return started;
}
}
/**
* Is there something left to do?
* As long as we expect new work to come, this is true;
* If we do not expect more work to come, it depends
* on what we have left.
*/
public boolean isWorkLeft()
{
synchronized(tasks)
{
return workExpected || !tasks.isEmpty();
}
}
/**
*/
public void waitStart()
throws InterruptedException
{
synchronized(tasks)
{
if (!started)
tasks.wait();
}
}
}
//
// Jacson - Text Filtering with Java.
// Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
// 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.1 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
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -