I intended to write a shared buffer between a producer and a consumer thread. Here are my codes:
class PQueue
{
Token token;
boolean flag = false; // false: add, true: poll
PQueue()
{
token = null;
}
synchronized void add(Token token)
{
if(flag == true)
{
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag = true;
notify();
this.token = token;
}
synchronized Token poll()
{
if(flag == false)
{
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
flag = false;
notify();
return this.token;
}
}
I am new to multithreading. Is there any potential concurrency bugs? Is this a “standard/common” way to achieve this goal? Or is there any much simpler and more efficient way?
Take a look at the
java.util.concurrentpackage, in particular theBlockingQueueinterface and the classes that implement it. These are meant for passing messages from one thread to another. TheSynchronousQueueis exactly what you’re trying to implement.Your own implementation has a few flaws. For one, both the shared variables should be declared
volatileto ensure that changes on one thread are seen by the other. And yourif (flag == false)andif (flag == true)tests should actually bewhileloops, becausewait()can wake up spuriously whennotify()hasn’t actually been called.Rather than having a separate flag variable, I’d suggest just setting the token to null to indicate that there’s no object. And rather than catching, printing, and blindly continuing in the face of
InterruptedException, I’d recommend letting both methods just throw that exception if it happens. These are blocking methods, and it’s the caller’s responsibility to handle the possibility of a blocking method being interrupted.Also, I don’t know what your
Tokenclass is, but nothing in your queue actually depends on its type. It’d make more sense to define a genericPQueue<T>, and then usePQueue<Token>if you want to pass tokens.