serverthread.java
来自「主要是对串口驱动的的一些控制源码!!! 在下载javacomm20-win32」· Java 代码 · 共 166 行
JAVA
166 行
package de.fhm.jkf.thread.cl;
/*
* <br><br><center><table border="1" width="80%"><hr>
* <strong><a href="http://jkf.sourceforge.net">The JKF Project</a></strong>
* <p>
* Copyright (C) 2002 by Theodor Willax
* <p>
* 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.
* <p>
* 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.
* <p>
* You should have received a copy of the <a href="http://www.gnu.org/copyleft/lesser.html">
* GNU Lesser General Public License</a> along with this library; if not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
* <hr></table></center>
*/
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
/**
* A <code>ServerThread</code> is a <code>Thread</code> which completes
* a task if one is given, or waits so long until it gets one.
*
* @author Theodor Willax
*/
public class ServerThread extends Thread {
/**
* The task this <code>ServerThread</code> has to do.
*/
private InterruptableTask task = null;
/**
* Is this <code>ServerThread</code> busy?
*/
private boolean busy = false;
/**
* Is this <code>ServerThread</code> interrupted?
*/
private boolean isInterrupted = false;
/**
* This objects are listener on this <code>ServerThread</code>.
*/
private Collection listeners = new Vector();
/**
* Creates a new <code>ServerThread</code> with the given name.
*
* @param name The name for this thread.
*/
public ServerThread(String name) {
super(name);
}
/**
* As long as this <code>ServerThread</code> is not interrupted,
* he calls the <code>run</code> method on the actual task. Is
* no task available, this <code>ServerThread</code> waits for
* a new task.
*/
public synchronized void run() {
while (!isInterrupted) {
while (task == null) {
try{
wait();
}catch(InterruptedException ignored) {}
}
try {
task.start();
System.out.println("Starting task");
task.run();
System.out.println("Finished task");
} catch (Exception e) {
task.handleException(e);
} finally {
taskReady();
}
}
}
/**
* Gives this <code>ServerThread</code> a new task to run. Calls
* internally <code>notify</code>, so the thread starts to work.
*/
public synchronized void runTask(InterruptableTask t) {
busy = true;
task = t;
notify();
}
/**
* Interrupts the work on the actual task. This <code>ServerThread</code>
* delegates the call to the task. The task itself is responsible for
* interrupting at a defiend position.
*
* @see de.fhm.jkf.thread.cl.InterruptableTask#interrupt
*/
public void interruptTask() {
if (task != null)
task.interrupt();
}
/**
* Shows if this <code>ServerThread</code> is busy.
*
* @return true if this <code>ServerThread</code> is busy, false otherwise.
*/
public boolean isBusy() {
return busy;
}
/**
* Adds listeners which like to be notified, if a task
* is ready.
*/
public void addTaskReadyListener(TaskReadyListener l) {
listeners.add(l);
}
/**
* Removes listeners from, whick liked to be notified, if a task
* was ready.
*/
public void removeTaskReadyListener(TaskReadyListener l) {
listeners.remove(l);
}
/**
* Interrupts this <code>ServerThread</code>, not only the task.
*/
public void interrupt() {
super.interrupt();
isInterrupted = true;
}
/**
* Signals all listeners that work on this task has ended. Every
* listener gets a reference to this <code>ServerThread</code> so
* a new task can be added.
*/
protected void taskReady() {
for (Iterator it = listeners.iterator(); it.hasNext();) {
((TaskReadyListener) it.next()).taskReady(this);
}
busy = false;
task = null;
}
/**
* Returns the task which is executed at this moment
*
* @return the actual task.
*/
public InterruptableTask getTask() {
return task;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?