Java synchronized question

I'm new to Java Threads and synchronization.

Lets say I have:

public class MyClass(){

    public synchronized void method1(){ 
        //call method2();
    } 

    public synchronized void method2(){};

}
  1. What does it mean when I synchronize a method1() on an instance object? So when a thread acquired the lock when trying to access the synchronized method1(), does it prevent other threads to access another synchronized method2() from that same object?

  2. Lets say a thread acquires a lock when accessing method1(), but lets say that method1() makes a call to method2() which is also synchronized. Can this be possible? I mean are there any rules that can prevent method1() from calling method2()?

Thanks in advance.

6
задан Bozho 11 September 2010 в 21:06
поделиться