[concurrency-interest] Example using TimedCallable with PooledExecutor

Joseph Bowbeer jozart@csi.com
Fri, 6 Jun 2003 14:26:30 -0700


This is a multi-part message in MIME format.

------=_NextPart_000_09C1_01C32C37.9F9CC260
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

Michael,

TimedCallable is designed for Callables (which can be viewed as "super" =
Runnables).

For simple Runnables you can use thread.join() as follows:

  Thread thread =3D new Thread(runnable);
  thread.start();
  try {
    thread.join(timeout);
  } catch (InterruptedException ex) {
    /* Stop thread if we were interrupted or timed-out
     * while waiting for the result.
     */
    thread.interrupt();
    throw ex;
  }

This doubles the thread count in your system.  If you do this alot, I =
recommend handling the timeouts on a single timer thread.

Also, note that thread.interrupt() may not do anything -- depending on =
what the runnable is doing at the time and whether it was written to be =
responsive to interrupts.



----- Original Message -----=20
From: "Michael Mattox" <michael.mattox@verideon.com>
To: <concurrency-interest@altair.cs.oswego.edu>
Sent: Friday, June 06, 2003 8:31 AM
Subject: [concurrency-interest] Example using TimedCallable with =
PooledExecutor



I'm using a PooledExecuter but sometimes my threads hang forever and I =
want
to add a timeout.  I looked at the TimedCallable class but it's not =
clear
how to use it for my particular example (I tried the example in the
FutureResult class).  Can someone give me a hint on how to use this?

private boolean addMonitorToQueue(String monitorId) {
boolean success =3D false;
DispatcherThread thread;
try {
thread =3D new DispatcherThread(monitorId);
if (!queue.contains(thread)) {
try {
pool.execute(thread);
success =3D true;
log.debug("Added monitor " + monitorId + " to the queue.");
} catch (InterruptedException e) {
log.error("Could not add thread for monitor " + monitorId + " : ", e);
}
} else {
log.debug("Did not add monitor " + monitorId + " to the queue because
it's already in the queue. ");
}
} catch (TechnicalException e) {
log.error("Could not dispatch thread for monitor: " + monitorId, e);
}
return success;
}

Thanks!
Michael

------=_NextPart_000_09C1_01C32C37.9F9CC260
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1170" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY>
<DIV><FONT face=3DArial size=3D2>
<DIV>Michael,</DIV>
<DIV>&nbsp;</DIV>
<DIV>TimedCallable is designed for Callables (which can be viewed as =
"super"=20
Runnables).</DIV>
<DIV>&nbsp;</DIV>
<DIV>For simple Runnables you can use thread.join() as follows:</DIV>
<DIV>&nbsp;</DIV></FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; Thread thread =3D new=20
Thread(runnable);</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; thread.start();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; try {</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;=20
thread.join(timeout);</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; }&nbsp;</FONT><FONT face=3DArial =
size=3D2>catch=20
(InterruptedException ex) {</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; /* Stop thread if we =
were=20
interrupted or timed-out</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;&nbsp; * while =
waiting for the=20
result.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp;&nbsp; =
*/</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; =
thread.interrupt();</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp;&nbsp;&nbsp; throw =
ex;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>&nbsp; }</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>This doubles the thread count&nbsp;in =
your=20
system.&nbsp; If you do this alot, I recommend handling the timeouts on =
a single=20
timer thread.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>Also, note that thread.interrupt() may =
not do=20
anything -- depending on what the runnable is doing at the time and =
whether it=20
was written to be responsive to interrupts.</FONT></DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3DArial size=3D2>----- Original Message ----- </FONT>
<DIV><FONT face=3DArial size=3D2>From: "Michael Mattox" &lt;</FONT><A=20
href=3D"mailto:michael.mattox@verideon.com"><FONT face=3DArial=20
size=3D2>michael.mattox@verideon.com</FONT></A><FONT face=3DArial=20
size=3D2>&gt;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>To: &lt;</FONT><A=20
href=3D"mailto:concurrency-interest@altair.cs.oswego.edu"><FONT =
face=3DArial=20
size=3D2>concurrency-interest@altair.cs.oswego.edu</FONT></A><FONT =
face=3DArial=20
size=3D2>&gt;</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Sent: Friday, June 06, 2003 8:31 =
AM</FONT></DIV>
<DIV><FONT face=3DArial size=3D2>Subject: [concurrency-interest] Example =
using=20
TimedCallable with PooledExecutor</FONT></DIV></DIV>
<DIV><FONT face=3DArial><BR><FONT size=3D2></FONT></FONT></DIV><BR><FONT =
face=3DArial=20
size=3D2>I'm using a PooledExecuter but sometimes my threads hang =
forever and I=20
want<BR>to add a timeout.&nbsp; I looked at the TimedCallable class but =
it's not=20
clear<BR>how to use it for my particular example (I tried the example in =

the<BR>FutureResult class).&nbsp; Can someone give me a hint on how to =
use=20
this?<BR><BR>private boolean addMonitorToQueue(String monitorId) =
{<BR>boolean=20
success =3D false;<BR>DispatcherThread thread;<BR>try {<BR>thread =3D =
new=20
DispatcherThread(monitorId);<BR>if (!queue.contains(thread)) {<BR>try=20
{<BR>pool.execute(thread);<BR>success =3D true;<BR>log.debug("Added =
monitor " +=20
monitorId + " to the queue.");<BR>} catch (InterruptedException e)=20
{<BR>log.error("Could not add thread for monitor " + monitorId + " : ",=20
e);<BR>}<BR>} else {<BR>log.debug("Did not add monitor " + monitorId + " =
to the=20
queue because<BR>it's already in the queue. ");<BR>}<BR>} catch=20
(TechnicalException e) {<BR>log.error("Could not dispatch thread for =
monitor: "=20
+ monitorId, e);<BR>}<BR>return=20
success;<BR>}<BR><BR>Thanks!<BR>Michael<BR></FONT></BODY></HTML>

------=_NextPart_000_09C1_01C32C37.9F9CC260--