Hello,
I have a code piece that I am reviewing (using FindBug).
public class MyClass{
...
private BlockedQueue q = new LinkedBlockingQueue<MyData>(1000);
private static final batchSize = 1000;
public boolean testMethod(){
boolean done = false;
synchronized(q){
if(q.size == batchSize){
q.notify();
done = true;
}
}
return done;
}
When I run FindBug on this piece of code, it complains that -
This method performs synchronization an object that is an instance of a class from the java.util.concurrent package (or its subclasses). Instances of these classes have there own concurrency control mechanisms that are distinct from and incompatible with the use of the keyword synchronized.
If I comment out the synchronized code piece synchronized(q){
, it complains -
This method calls Object.notify() or Object.notifyAll() without obviously holding a lock on the object. Calling notify() or notifyAll() without a lock held will result in an IllegalMonitorStateException being thrown
How would I implement this method so that it passes FindBugs validation? Is the above implementation right one for notification in cases for concurrent classes?
Thank you.