Channels ▼
RSS

JVM Languages

Multithreading, Java, & OSGi

Source Code Accompanies This Article. Download It Now.


ThreadManager Design Points

I found many subtle and interesting design points in writing ThreadManager, and probably not all of them.

For instance, the major service provided by the ThreadManager is keeping track of each thread it starts so that they can all be stopped at some later time. This requires nothing more than keeping an accurate list of interruptible objects running on various threads. The list part is easy; the accurate part takes a bit of effort.

Each Interruptible is added to the list of running objects before its assigned Thread is started. This guarantees that once ThreadManager.run() is called for some Interruptible, both run() and interrupt() are called on it. It also makes it possible for interrupt() to be called first.

The second half of this problem is removing each Interruptible from the list when it exits, which happens when the run() method returns. This is handled by scheduling not the Interruptible itself but a private class called ManagedRunnable. ManagedRunnable has a run() method that wraps the call to Interruptible.run() in a try/finally block. The finally clause is invoked when run() returns—whether normally or with an exception—and removes the Interruptible from the list.

The kind of interrupt required varies, depending on what the code in the thread is doing. The Thread.interrupt() method isn't always appropriate. For example, while it will interrupt a thread waiting on Object.wait(), it won't interrupt a thread waiting on ServerSocket.accept(). The interrupt() method implementation—which is always called from a thread other than the one on which run() is called—must take appropriate action to interrupt the thread, whatever that might be. My prototype called for three different strategies.

My prototype used SWT for the UI. The application bundle launched a thread on which a loop polled and processed the SWT event queue. The interrupt method for this thread made use of the SWT Display.asyncExec() call to post a "quit" event back to the UI thread, causing that loop to exit. Swing provides similar facilities. Worst case, you could implement your own mechanism for passing the request between threads and check it in the event loop. (In that case, however, you might need a timeout on the call to poll for an event.)

Threads waiting on I/O are even easier; typically, simply closing the stream or socket on which they are waiting is sufficient. The close will cause the read(), accept(), or other call to return with an error. The thread performing the I/O simply needs to exit when this error occurs.

Worker threads performing computation can be the trickiest because there is not a good mechanism for interrupting CPU-bound work from another thread. The only option here is to have the thread periodically check to see if an interrupt bit has been set. It shouldn't do that too often or the overhead will take a toll, but it must do it frequently enough to keep the interrupt responsive.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video