It's a good idea to reuse a thread?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   zhang_joachimz
Posted On:   Friday, November 7, 2003 10:20 PM

I use Thread.interrupt() to awake a sleeping thread when has new task to do, simple code fragment like following: public class ReusableThread extends Thread { private final Object lock = new Object(); private Runnable mutableTask = null; private boolean stopped = false; public void setNewTask(Runnable newTask) { synchronized (lock) { this.mutableTask = newTask; } this.interrupt(); } public void shutdown() { stopped = true; this.interrupt(); } public void run() { while (!stopped) { if (null == newTask) { try { Thread.sleep(Int   More>>

I use Thread.interrupt() to awake a sleeping thread when has new task to do, simple code fragment like following:


			
public class ReusableThread extends Thread
{
private final Object lock = new Object();

private Runnable mutableTask = null;
private boolean stopped = false;

public void setNewTask(Runnable newTask)
{
synchronized (lock)
{
this.mutableTask = newTask;
}
this.interrupt();
}
public void shutdown()
{
stopped = true;
this.interrupt();
}
public void run()
{
while (!stopped)
{
if (null == newTask)
{
try {
Thread.sleep(Integer.MAX_VALUE);
} catch (InterruptedException e)
{
// ignored, occur when awake it
}
} // end if xxx
Runnable r = null;
synchronized (lock)
{
r = mutableTask;
mutableTask = null;
} end synchronized(lock)
if (null != r) r.run();
} // end while (!stopped)
} // end run()
} // end class


I'm confused by the mechanism of notify and wait. But I haven't seen anyone do like that, Thanks for your comments.

   <<Less

Re: It's a good idea to reuse a thread?

Posted By:   Joe_Cheng  
Posted On:   Tuesday, November 25, 2003 01:17 PM

Here's the notify/wait version. (Note that your code makes it possible for tasks to be lost--for example if setNewTask() is called twice before the thread has had a chance to finish whatever it's doing, then the first of those two tasks will be lost. Maybe that's what you intended. If not, you'd probably be better off with a queue of tasks that is shared among many threads.)


public class ReusableThread extends Thread
{
private final Object lock = new Object();

private Runnable mutableTask = null;
private boolean stopped = false;

public void setNewTask(Runnable newTask)
{
synchronized (lock)
{
this.mutableTask = newTask;
lock.notifyAll();
}
}
public void shutdown()
{
stopped = true;
this.interrupt();
}
public void run()
{
while (!stopped)
{
Runnable r = null;

synchronized (lock)
{
while (mutableTask == null)
lock.wait();
r = mutableTask;
mutableTask = null;
}

r.run();

} // end while (!stopped)
} // end run()
} // end class

Re: It's a good idea to reuse a thread?

Posted By:   Edward_Harned  
Posted On:   Saturday, November 8, 2003 07:57 AM

Notify() and Wait() are what multi-threading is all about.


Go over to the Sun advanced forum and search for the above. There are many examples of how to do notify/wait.


http://forum.java.sun.com/forum.jsp?forum=4

About | Sitemap | Contact