<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -9,7 +9,8 @@
 # For a project with a C extension, the following would be a good set of
 # exclude patterns (uncomment them if you want to use them):
 # *.[oa]
-# *~
+*~
+*.sw[op]
 announcement.txt
 coverage
 doc</diff>
      <filename>.gitignore</filename>
    </modified>
    <modified>
      <diff>@@ -57,13 +57,17 @@ module Servolux::Threaded
 
     before_starting if self.respond_to?(:before_starting)
     @activity_thread_running = true
+    @activity_thread_iterations = 0
     @activity_thread = Thread.new {
       begin
         loop {
           sleep interval if running?
           break unless running?
           run
+          @activity_thread_iterations += 1
+          break if finished_iterations?
         }
+        @activity_thread_running = false
       rescue Exception =&gt; err
         @activity_thread_running = false
         logger.fatal err unless err.is_a?(SystemExit)
@@ -96,6 +100,20 @@ module Servolux::Threaded
     self
   end
 
+  # Wait on the activity thread.  If the thread is already stopped, this
+  # method will return without taking any action.  Otherwise, this method
+  # does not return until the activity thread has stopped, or a specific
+  # number of iterations has passed since this method was called.
+  #
+  def wait( limit = nil )
+    return self unless running?
+    start_waiting_iterations = self.iterations
+    loop {
+      break unless running?
+      break if limit and self.iterations &gt; ( start_waiting_iterations + limit )
+    }
+  end
+
   # If the activity thread is running, the calling thread will suspend
   # execution and run the activity thread. This method does not return until
   # the activity thread is stopped or until _limit_ seconds have passed.
@@ -115,6 +133,22 @@ module Servolux::Threaded
     @activity_thread_running
   end
 
+  # Returns +true+ if the activity thread has finished its maximum
+  # number of iterations or the thread is no longer running.
+  # Returns +false+ otherwise.
+  #
+  def finished_iterations?
+    if running? then
+      if @activity_thread_maximum_iterations and
+         (@activity_thread_iterations &gt;= @activity_thread_maximum_iterations) then
+        return true
+      end
+    else
+      return true
+    end
+    return false
+  end
+
   # Returns the status of threaded object.
   #
   #    'sleep'    : sleeping or waiting on I/O
@@ -145,6 +179,28 @@ module Servolux::Threaded
     @activity_thread_interval
   end
 
+  # Sets the maximum number of invocations of the threaded object's
+  # 'run' method
+  #
+  def maximum_iterations=( value )
+    raise ArgumentError, &quot;maximum iterations must be &gt;= 1&quot; unless value.to_i &gt;= 1
+    @activity_thread_maximum_iterations = value
+  end
+
+  # Returns the maximum number of invocations of the threaded
+  # object's 'run' method
+  #
+  def maximum_iterations
+    @activity_thread_maximum_iterations || 0
+  end
+
+  # Returns the number of iterations so far of the threaded object's
+  # 'run' method.
+  #
+  def iterations
+    @activity_thread_iterations || 0
+  end
+
   # :stopdoc:
   #
   # The JRuby platform has an implementation error in it's Thread#join</diff>
      <filename>lib/servolux/threaded.rb</filename>
    </modified>
    <modified>
      <diff>@@ -90,6 +90,7 @@ describe Servolux::Threaded do
     lambda { obj.join }.should raise_error(RuntimeError, 'ni')
   end
 
+
   it &quot;complains loudly if you don't have a run method&quot; do
     obj = base.new
     obj.start
@@ -100,6 +101,23 @@ describe Servolux::Threaded do
 
     lambda { obj.join }.should raise_error(NotImplementedError, 'The run method must be defined by the threaded object.')
   end
+
+  it &quot;stops after a limited number of iterations&quot; do
+    klass = Class.new( base ) do
+      def run() ; end
+    end
+    obj = klass.new
+    obj.maximum_iterations = 5
+    obj.iterations.should == 0
+    obj.start
+    obj.wait
+    obj.iterations.should == 5
+  end
+
+  it &quot;complains loudly if you attempt to set a maximum number of iterations &lt; 1&quot; do
+    obj = base.new
+    lambda { obj.maximum_iterations = -1 }.should raise_error( ArgumentError, &quot;maximum iterations must be &gt;= 1&quot; )
+  end
 end
 
 # EOF</diff>
      <filename>spec/threaded_spec.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>061af873387a4826d1a9dea77cc4a2b3f0a1068f</id>
    </parent>
  </parents>
  <author>
    <name>Jeremy Hinegardner</name>
    <email>jeremy@hinegardner.org</email>
  </author>
  <url>http://github.com/TwP/servolux/commit/fb727dd8616327855b302ae182b4b0aa63b4ec0f</url>
  <id>fb727dd8616327855b302ae182b4b0aa63b4ec0f</id>
  <committed-date>2009-07-04T11:42:04-07:00</committed-date>
  <authored-date>2009-07-04T11:42:04-07:00</authored-date>
  <message>Add in iteration limiting ability</message>
  <tree>57b7dbf72174de8784ba32385fa6d1275eda4230</tree>
  <committer>
    <name>Jeremy Hinegardner</name>
    <email>jeremy@hinegardner.org</email>
  </committer>
</commit>
