title: Calling Thread.sleep inside an actor
body: <p>I have a function retry which basically looks like this (simplified):</p>
  
  <pre><code>object SomeObject {
   def retry[T](n: Int)(fn: =&gt; T): Option[T] = {
      val res = try {
        Some(fn)
      } catch {
        case _: Exception =&gt; None
      }
  
      res match {
        case Some(x) =&gt; Some(x)
        case None =&gt; 
          if (n &gt; 1) 
            //make it sleep for a little while
            retry(n - 1)(fn) 
          else None
      }
    }
  }
  </code></pre>
  
  <p>I need to make some pause between the attempts. As I was told, it's not acceptable to call <code>Thread.sleep(123)</code> inside an actor:</p>
  
  <pre><code>class MyActor extends Actor {
    //......
    def someFunc = {
      Thread.sleep(456) // it's not acceptable in an actor, there is another way to do it
    }
  
  }
  </code></pre>
  
  <p>Obviously, I don't know whether or not a client will use <code>SomeObject.retry</code> inside an actor:</p>
  
  <pre><code>class MyActor extends Actor {
    //......
    def someFunc = {
      SomeObject.retry(5)(someRequestToServer) // ops, SomeObject.retry uses Thread.sleep!
    }
  
  }
  </code></pre>
  
  <p>So if I just add:</p>
  
  <pre><code>res match {
        case Some(x) =&gt; Some(x)
        case None =&gt; 
          if (n &gt; 1) 
            //make it sleep for a little while
            Thread.sleep(123) // ops, what if it's being called inside an actor by a client?!
            retry(n - 1)(fn) 
          else None
      }
    }
  </code></pre>
  
  <p>it won't be sensible, will it? If not, what do I do?</p>