Why is this thread allowing another one to access its synchronized method?
<p>I have the following codes. I expected one thread to execute its synchronized method completely and then allow another one to access the same method. However, this is not the case.</p>

<pre><code>public class Threads  {

    /**
     * @param args
     */
    public static void main(String[] args) {
        //Thread Th = new Threads();
        Thread th = new Thread (new thread1 ());
        th.start();
        Thread th1 = new Thread (new thread1 ());
        th1.start();
    }
}



class thread1 implements Runnable{
    String name = "vimal";

    public void run() {
        System.out.println("Runnable "+this.name);
        setNAme("Manish");

    }

    public synchronized void setNAme(String name){
        try {
            System.out.println("Thread "+Thread.currentThread().getName());
            wait(1000);
            this.name = name;
            System.out.println("Name "+this.name);

        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }   
}
</code></pre>

<p>I have one output as </p>

<pre><code>Runnable vimal
Thread Thread-0
Runnable vimal
Thread Thread-1
Name Manish
Name Manish
</code></pre>

<p>What is the use of synchronized here and how do I make my method to run completely before another accesses it?</p>
