0
public class ThreadTest {
     public static synchronized void m2() {
         System.out.println("static sync m2");
         System.out.println("current"+Thread.currentThread());
         try { Thread.sleep(2000); }
         catch (InterruptedException ie) {}
     }

    public static void main(String[] args) throws InterruptedException {
        Thread t3 = new Thread() {
            public void run() {
                ThreadTest.m2();
            }
        };
        t3.start();
        t3.sleep(2000);
        Thread.sleep(500); // which thread are we sleeping here?
        System.out.println("t3.getState"+t3.getState());
    }
}

If we create another thread t1 and access ThreadTest.m2(); inside of this? yes this will be allowed, why this is static and it class level. But if we have non-static methods, then Thread 1 and 2 are not allowed to access the method

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • What do you mean by "allowed" or "not allowed" to access the method? Are you referring to whether or not the thread becomes blocked when trying to call m2()? – Shirik Jun 24 '11 at 04:37

1 Answers1

0

Please check this out: Java synchronized static methods: lock on object or class

Regards.

Edit: in particular, this answer: Java synchronized static methods: lock on object or class

Community
  • 1
  • 1
Martín Schonaker
  • 7,273
  • 4
  • 32
  • 55