title: understanding nested locks in java
body: <p>i want to understand this:</p>
  
  <pre><code>String string;
  public Test(String string){
       this.string = string;
       .....
  }
  
  public void foo(newObjectPerCall o) {
          synchronized (o) {
              //each thread can enter here because the object passed is always different
              ....
              synchronized (string) {
                  // acquire this lock if the String is free.
              }
          }
      }
  
  public synchronized void function() {
  
  }
  
  public static void main(){
      Test test = new Test("hello");
      for(int i = 0; i &lt; 10;i++){
           WorkerThread workerThread = new WorkerThread(test);
           workerThread.start(); 
     }
   }
  </code></pre>
  
  <p>thread class</p>
  
  <pre><code>public class WorkerThread extends Thread {
  
      private Test test;
  
      public WorkerThread(Test test) {
          this.test = test;
      }
  
      @Override
      public void run() {
          while (true) {
              test.foo(new Object());
              try {
                  sleep(3000);
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
              test.function();
          }
      }
  }
  </code></pre>
  
  <p>my doubts are:</p>
  
  <ul>
  <li><em>if one thread acquire the inner lock, other threads that acquired the outer lock, will remains inside the <code>foo</code> function?</em></li>
  <li><em>When on thread ,for some reasons, remains outside the <code>foo</code> function, can this one acquire the lock of test instance inside <code>function</code>?</em></li>
  </ul>