473,322 Members | 1,714 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

reuse a thread which is no longer "alive"

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
7 7832
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
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
by: Jon Skeet [C# MVP] | last post by:
Please excuse the cross-post - I'm pretty sure I've had interest in the article on all the groups this is posted to. I've finally managed to finish my article on multi-threading - at least for...
5
by: EdgarBM | last post by:
Hi, I'm working with .NET Remoting. I have a problem unregistering the server channel when I try to reuse it closing and reopening it in the same application. The second time I try to get an...
1
by: M D | last post by:
If you want more details you will have to reference the VS.Net example ConsoleChat for Networking How-to: -- http://go.microsoft.com/fwlink/?linkid=3480&path=/quickstart/howto/sampl...
12
by: MuZZy | last post by:
Hi, Sorry for a repeated post but i didn't receive an answer and will try to re-phrase my question: How do i close an additional thread from the main thread, if this additional thread is stuck...
2
by: arfinmail | last post by:
I have 4 applications which are supposed to run 24/7. How can I make a monitor application to know if they're running fine? The best I can come up with is making them update a file/DB every X...
16
by: a | last post by:
Hi everybody, My config: Win XP (or 2003), Apache 2.0.54, PHP 5.1.2. I have been trying to handle the case of a lenghty opearation on the server while providing the user with feedback and...
6
by: alessandro | last post by:
Hi all, This is my framework for create TCP server listening forever on a port and supporting threads: import SocketServer port = 2222 ip = "192.168.0.4"
4
by: Morgan Cheng | last post by:
Days ago, I post a question on how to make SoapHttpClientProtocol instance make new TCP connection for each web service request. Now, I found how. SoapHttpClientProtocol has a protected method...
4
by: Sin Jeong-hun | last post by:
I don't get the message so it's hard to debug that, but some of my clients report that they get "The underlying connection was closed unexpectedly" exception. According to this site (http://...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.