Q:

Improving concurrency in a multithreading example

Locking mechanism - Each method runs a Queue for tracking in-process keys being proceeding for that method.
    private final static Object LOCK = new Object();
    private final static Set<String> busy1Records = new HashSet<String>();
    private final static Set<String> busy2Records = new HashSet<String>();

    public void waitToWorkOn(int i, String key) {
        synchronized(LOCK) {

          switch(i) {
            case 1 : while (busy1Records.contains(key)) {
                        LOCK.wait(); //go to sleep
                     }
                     busy1Records.add(key);  
                     break;
            case 2 : while (busy2Records.contains(key)) {
                     LOCK.wait(); //go to sleep
                     }
                     busy2Records.add(key);  
                     break;
          }//end switch

       } // end sychronized                    
    }

    public void doneWith(int i, String key) {
        synchronized(LOCK) {

        switch(i) { 

          case 1:
              busy1Records.remove(key);
              LOCK.notifyAll(); 
              break;
          case 2:
              busy2Records.remove(key);
              LOCK.notifyAll(); 
              break;

        }

To use these Sets , there are these methods in that class.    
private void method1(String key) {

try {

waitToWorkOn(1,key);
// Do stuffs here for key   

}catch(Exception e)  {..}
finally {
doneWith(1,key);
}

}

private void method2(String key) {

try {

waitToWorkOn(2,key);    
// Do stuffs here for key

}catch(Exception e)  {..}
finally {
doneWith(2,key);
}

}

Consider there are 3 Threads [ Each thread works on a new instance of this class]
T1 - key = X
T2 - key = X
T3 - key = Z

So when T1 is in method1, it acquires the LOCK and adds key to its queue for method1 and releases the LOCK. But it has not yet called doneWith method and then another Thread T2 tries to insert same entity X. So it acquires LOCK in waitToWorkON and since it finds X already in Queue1 , so it has to wait inside. Then a Thread T3 comes that has to call method1 on third entity Z which is independent of X. But it won't be able to acquire LOCK since T2 is waiting and holds the LOCK. 
How can I let T3 to proceed with method1 for Z without waiting for T2 waiting on X there ?
T1 still has not released X from Queue.

A:

Thanks @Hamlet and @assylias for answers

@fortm LOCK.wait() releases the lock, which is reacquired when wait returns. So yes, if thread 1 is waiting, thread 2 can enter the synchronized block. – assylias 21 mins ago 
  1  up voted 
Ok, i understand what you want to achieve. Yes, when lock calls wait,
  it releases lock and thread goes to wait queue until it will be
  signaled. After that the thread moves to ready queue and WAITs to
  reacquire the lock. – Hamlet Hakobyan 2 mins ago

