Try this:
import java.util.LinkedList;
import java.util.List;
public final class TaskQueue {
private final List mQueue;
private boolean mIsShutdown;
public TaskQueue() {
mQueue = new LinkedList();
}
public Runnable dequeue() throws InterruptedException {
synchronized (mQueue) {
while (!isShutdown() && mQueue.isEmpty()) {
mQueue.wait();
}
if (mQueue.isEmpty()) {
return null;
}
else {
Runnable result = (Runnable) mQueue.get(0);
mQueue.remove(result);
return result;
}
}
}
public void enqueue(Runnable task) {
synchronized (mQueue) {
mQueue.add(task);
mQueue.notifyAll();
}
}
public boolean isShutdown() {
synchronized (mQueue) {
return mIsShutdown;
}
}
public void shutdown() {
synchronized (mQueue) {
mIsShutdown = true;
mQueue.notifyAll();
}
}
}
public final class Executor extends Thread {
private final TaskQueue mQueue;
public static Executor startRunner(TaskQueue queue) {
Executor runner = new Executor(queue);
runner.start();
return runner;
}
private Executor(TaskQueue queue) {
mQueue = queue;
}
public void run() {
while (!mQueue.isShutdown()) {
try {
Runnable task = mQueue.dequeue();
if (task != null) {
task.run();
}
}
catch (Throwable e) {
// NOTE: tasks *must* handle their own errors
e.printStackTrace();
}
}
}
}
import java.util.ArrayList;
import java.util.Collection;
public final class ExecutorPool {
private final TaskQueue mQueue;
private final Collection mPool;
public ExecutorPool(int numThreads) {
if (numThreads < 1) {
throw new IllegalArgumentException("Must use at least one thread");
}
mQueue = new TaskQueue();
mPool = new ArrayList(numThreads);
for (int i = 0; i < numThreads; i++) {
mPool.add(Executor.startRunner(mQueue));
}
}
public void execute(Runnable task) {
mQueue.enqueue(task);
}
public void shutdown() {
mQueue.shutdown();
}
}
import java.util.Date;
import junit.framework.TestCase;
public final class ExecutorPoolTest extends TestCase {
private ExecutorPool mPool;
public ExecutorPoolTest(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(slow);
mPool.execute(fast);
try {
synchronized (this) {
this.wait(1000);
}
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
super.assertNotNull(slow.mFinished);
super.assertNotNull(fast.mFinished);
super.assertTrue(slow.mFinished.compareTo(fast.mFi nished) > 0);
}
private static final class SlowTask implements Runnable {
private Date mFinished;
public void run() {
try {
synchronized (this) {
this.wait(500);
}
}
catch (InterruptedException e) {
throw new RuntimeException(e);
}
mFinished = new Date();
}
}
private static final class FastTask implements Runnable {
private Date mFinished;
public void run() {
mFinished = new Date();
}
}
}
"sayoyo" <do**********@yahoo.com> wrote in message
news:UN*******************@news20.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*****@despammed.com> a écrit dans le message de
news:gC*****************@fe2.texas.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**********@yahoo.com> wrote in message
news:Uz********************@news20.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