title: Why can't we call Thread#sleep() directly inside a Lambda function?
body: <p>The below code is giving me a compile time error:</p>
  
  <pre><code>Thread t2 = new Thread(() -&gt; {
      try { 
          sleep(1000);
      } 
      catch (InterruptedException e) {}
  });
  </code></pre>
  
  <blockquote>
    <p><strong>The method sleep(int) is undefined for the type A</strong>  (where A is my class name).</p>
  </blockquote>
  
  <p>Whereas, when I use an anonymous inner class, there is no compile time error:</p>
  
  <pre><code>Thread t1 = new Thread(){
      public void run(){
          try {
              sleep(1000);
          } catch (InterruptedException e) {}
      }
  };
  </code></pre>
  
  <p>The below code also works fine:</p>
  
  <pre><code>Thread t3 = new Thread(() -&gt; System.out.println("In lambda"));
  </code></pre>
  
  <p>How do things work inside a lambda expression body? Please help.</p>
  
  <p>From many answers, I can see that the error can be resolved using <code>Thread.sleep(1000)</code> in my first approach. However, I'd really appreciate if someone could explain to me how scope and context work in a lambda expression.</p>