By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,983 Members | 2,082 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,983 IT Pros & Developers. It's quick & easy.

reuse a thread which is no longer "alive"

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
No
"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

Jul 17 '05 #2

P: n/a
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

Jul 17 '05 #3

P: n/a
"sayoyo" <do**********@yahoo.com> writes:
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.


There is a util-concurrent Java package which provides a thread pool
class. You hand a runnable to the thread pool, and it will look for
an unoccupied thread to execute that runnable. When the runnable is
done, the thread becomes available again.

Google for "util-concurrent".

Kai
Jul 17 '05 #4

P: n/a
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


Jul 17 '05 #5

P: n/a
Thanks you very much, and I will have a look on it:)

Sayoyo

"Kai Grossjohann" <kg******@eu.uu.net> a écrit dans le message de
news:86************@slowfox.de.uu.net...
"sayoyo" <do**********@yahoo.com> writes:
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.


There is a util-concurrent Java package which provides a thread pool
class. You hand a runnable to the thread pool, and it will look for
an unoccupied thread to execute that runnable. When the runnable is
done, the thread becomes available again.

Google for "util-concurrent".

Kai

Jul 17 '05 #6

P: n/a
Ok, i'm making a unit-tested component to do this, for fun. What are you
going to use it in?

"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



Jul 17 '05 #7

P: n/a
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



Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.