Henry Wong wrote:
Synchronization is based on objects -- not classes or methods. If two threads synchronize on the same object, they will block on each other -- otherwise, they will not.
Henry
For example, in your case you may save on sleeping time:
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
private static Object monitor = new Object();
public void method() {
synchronized (monitor) {
// Things to synchronize here..
System.out.println("THREAD NAME : " + Thread.currentThread().getName());
}
try {
Thread.sleep(1000000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
As alternative solution, you may move your Thread.sleep(..) code to another non-synchronized method.
Mark Butcher wrote:But then in what scenario should one use synchronized methods ??
Anil KumarHn wrote:If am not wrong synchronized method are mainly used to avoid deadlock situation where one object may use resource of other object .Even if we use sleep() lock is not released until the object completes its execution.
Correct me if am wrong.