Thanks. In the following code, which object is synchronized?
public class TestThread implements Runnable
new Thread(this,"Test 1").start();
new Thread(this,"Test 22").start();
synchronized public void run()
System.out.println((i++)+"....Sync block 1 :"+Thread.currentThread().getName());
public static void main(String args) throws InterruptedException
During the execution of a synchronized method, the thread holds the monitor for that method's object, or if the method is static, it holds the monitor for that method's class. If another thread is executing the synchronized method, your thread is blocked until that thread releases the monitor (by either exiting the method or by calling wait()).
Sorry ramjeev, musthafa is right. "this" is the method's object.
See Java Lanmguage ref:
18.104.22.168 synchronized Methods
A synchronized method acquires a monitor (§17.1) before it executes. For a class (static) method, the monitor associated with the Class object for the method's class is used. For an instance method, the monitor associated with this (the object for which the method was invoked) is used.