Try this:
import java.util.Linke dList;
import java.util.List;
public final class TaskQueue {
private final List mQueue;
private boolean mIsShutdown;
public TaskQueue() {
mQueue = new LinkedList();
}
public Runnable dequeue() throws InterruptedExce ption {
synchronized (mQueue) {
while (!isShutdown() && mQueue.isEmpty( )) {
mQueue.wait();
}
if (mQueue.isEmpty ()) {
return null;
}
else {
Runnable result = (Runnable) mQueue.get(0);
mQueue.remove(r esult);
return result;
}
}
}
public void enqueue(Runnabl e task) {
synchronized (mQueue) {
mQueue.add(task );
mQueue.notifyAl l();
}
}
public boolean isShutdown() {
synchronized (mQueue) {
return mIsShutdown;
}
}
public void shutdown() {
synchronized (mQueue) {
mIsShutdown = true;
mQueue.notifyAl l();
}
}
}
public final class Executor extends Thread {
private final TaskQueue mQueue;
public static Executor startRunner(Tas kQueue queue) {
Executor runner = new Executor(queue) ;
runner.start();
return runner;
}
private Executor(TaskQu eue queue) {
mQueue = queue;
}
public void run() {
while (!mQueue.isShut down()) {
try {
Runnable task = mQueue.dequeue( );
if (task != null) {
task.run();
}
}
catch (Throwable e) {
// NOTE: tasks *must* handle their own errors
e.printStackTra ce();
}
}
}
}
import java.util.Array List;
import java.util.Colle ction;
public final class ExecutorPool {
private final TaskQueue mQueue;
private final Collection mPool;
public ExecutorPool(in t numThreads) {
if (numThreads < 1) {
throw new IllegalArgument Exception("Must use at least one thread");
}
mQueue = new TaskQueue();
mPool = new ArrayList(numTh reads);
for (int i = 0; i < numThreads; i++) {
mPool.add(Execu tor.startRunner (mQueue));
}
}
public void execute(Runnabl e task) {
mQueue.enqueue( task);
}
public void shutdown() {
mQueue.shutdown ();
}
}
import java.util.Date;
import junit.framework .TestCase;
public final class ExecutorPoolTes t extends TestCase {
private ExecutorPool mPool;
public ExecutorPoolTes t(String arg0) {
super(arg0);
}
protected void setUp() throws Exception {
super.setUp();
mPool = new ExecutorPool(2) ;
}
protected void tearDown() throws Exception {
super.tearDown( );
mPool.shutdown( );
}
public void testExecute() {
SlowTask slow = new SlowTask();
FastTask fast = new FastTask();
mPool.execute(s low);
mPool.execute(f ast);
try {
synchronized (this) {
this.wait(1000) ;
}
}
catch (InterruptedExc eption e) {
throw new RuntimeExceptio n(e);
}
super.assertNot Null(slow.mFini shed);
super.assertNot Null(fast.mFini shed);
super.assertTru e(slow.mFinishe d.compareTo(fas t.mFinished) > 0);
}
private static final class SlowTask implements Runnable {
private Date mFinished;
public void run() {
try {
synchronized (this) {
this.wait(500);
}
}
catch (InterruptedExc eption e) {
throw new RuntimeExceptio n(e);
}
mFinished = new Date();
}
}
private static final class FastTask implements Runnable {
private Date mFinished;
public void run() {
mFinished = new Date();
}
}
}
"sayoyo" <do**********@y ahoo.com> wrote in message
news:UN******** ***********@new s20.bellglobal. com...
Yes, Please, I would like to have a look on it.
And do you know the internal design of a "Thread", when you creates a
"Thread" you can put a "Runnable" as argument. What the "Thread" does
exactly with the "Runnable"?
What is different between using "start()" and "run()"?
Thanks you very much:)!!!!
Sayoyo
"Jeff Rhines" <jr*****@despam med.com> a écrit dans le message de
news:gC******** *********@fe2.t exas.rr.com... What i've done in the past is use a queue that i fill with runnables,
and have dequeue threads that wait for something to be in the queue. When
there is a runnable in the queue, one of the threads grabs it and executes it,
then goes back to trying to dequeue something out of the queue. Cool,
eh? Let me know if you need more implementation details.
Jeff
"sayoyo" <do**********@y ahoo.com> wrote in message
news:Uz******** ************@ne ws20.bellglobal .com... Hi,
Is there some way that we can reuse a thread by replacing the runnable
object of the thread? like a thread is not "alive" anymore, then we
remove the runnable object(is it possible????) and then run it again.
Thanks you very much
Sayoyo