I need to wait until a list of thread terminate, but my code works only if the Sleep is not constant i want to knwo why, here my Class test :
If i change Thread.sleep(200); ---> Thread.sleep(i*b); its works fine !?
public class TestThread {
    public static void main(String[] args) {
        Object lock = new Object();     
        for ( int p=0; p<10; p++) {     
            final int i=p;
            new Thread(new Runnable() {                 
                @Override
                public void run() {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("notify "+i);
                    synchronized(lock){                             
                        lock.notify();
                    }   
                }
            }).start();             
        }
        Integer counter=0;
        synchronized (lock) {
            try {               
                while(true){
                    System.out.println("Before wait");                  
                    if (counter==10)//wait until all threads ends
                        break;
                    lock.wait();
                    counter += 1;
                    System.out.println("After wait "+counter);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }   
        }
        System.out.println("End");
    }
}
The result with notifyAll()
Before wait
notify 3
notify 7
After wait 1
Before wait
notify 1
notify 2
notify 0
After wait 2
Before wait
notify 4
After wait 3
Before wait
After wait 4
Before wait
notify 5
After wait 5
Before wait
notify 6
notify 8
After wait 6
Before wait
notify 9
After wait 7
Before wait
After wait 8
Before wait
And the process is not terminated
 
     
     
     
     
    