473,668 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7863
No
"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

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**********@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

Jul 17 '05 #3
"sayoyo" <do**********@y ahoo.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*****@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


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**********@y ahoo.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**********@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



Jul 17 '05 #7
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



Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

77
5262
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 the moment. I'd be *very* grateful if people with any interest in multi-threading would read it (even just bits of it - it's somewhat long to go through the whole thing!) to check for accuracy, effectiveness of examples, etc. Feel free to mail...
5
7421
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 instance of the same channel it returns an exception with socket code 10048 (already in use). My server code is, ....for openning:
1
1471
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 es/net/TCPUDP/Chat.src In trying to put it into a Windows form (c#.net, VS 2002) this is my main:
12
2123
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 waiting for a blocking operation, eg. if in this additional thread i wait for a Tcp connection: Socket s = tcpListener.AcceptSocket();
2
1237
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 amount of time and have the monitor app check that mark. If the mark haven't updated then it means the program stopped. The programs do their thing multithreaded so we don't have the problem of waiting for the main thread to finish. But this is...
16
2910
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 ability to cancel, and nothing seems to work so far. The following code is one attempt to address this, and it is called as result of a POST:
6
16176
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
11407
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 GetWebRequest(System.Uri uri) which returns a WebRequest instance. Though MSDN doesn't make clear statement. I experiment and prove that SoapHttpClientProtocol use this method to create HttpWebRequest for HTTP request. So, I override this...
4
4564
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:// www.dotnetspider.com/resources/2596-e-underlying-connection-was-closed-A-connection.aspx), it's a bug of .NET 2.0, and the author suggests that we use KeepAlive=false until Microsoft fixes it. It seems like almost 5 years have passes since the...
0
8890
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8791
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8575
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7398
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5677
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2784
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2018
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1783
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.