0

static synchronized method locks in the class object and the synchronized method locks the current instance of an object. I am not able to relate to any real-world examples and cant clearly distinguish the difference between them. So if possible give me some examples that I can relate to concept.

Hemesh
  • 107
  • 9

1 Answers1

0

Let's look at Java's Thread class. It offers self explanatory examples.

Synchronized static method - single JVM wide id generator

private static int threadInitNumber;
private static synchronized int nextThreadNum() {
    return threadInitNumber++;
}

Synchronized instance method - lock each thread instance:

public synchronized void start() {
  ...
}
Delta George
  • 2,442
  • 2
  • 17
  • 11