Assuming that DecibelSample
is a class, this is not valid Java code.
Modify your code like this in order to get rid of the compile error:
synchronized (DecibelSample.class) {}
Your code won't work because synchronized
needs some instance to lock against. In the modified example above it will use the Class
instance.
You could also change it to synchronized (this) {}
in which case it would use the instance of the class your method is in as the lock.
Third option is to define arbitrary object to use as a lock, for instance like this:
private static final Object LOCK = new Object();
...
public void foo() {
synchronized(LOCK) {}
}
This would probably be the best approach, since locking against the current instance or class instance has some disadvantages. See this SO answer for more details:
More information about the synchronized
keyword can be found in Java Language Specification.