Reputation: 10555
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?
Upvotes: 2
Views: 3722
Reputation: 33534
1. Try using the thread-safe
classes and interfaces from java.util.concurrent
Package.
2. Use the BlockingQueue
Interface
along with the ArrayBlockingQueue
Class
.
Upvotes: 1
Reputation: 34563
Take a look at the java.util.concurrent
package, in particular the BlockingQueue
interface and the classes that implement it. These are meant for passing messages from one thread to another. The SynchronousQueue
is exactly what you're trying to implement.
Your own implementation has a few flaws. For one, both the shared variables should be declared volatile
to ensure that changes on one thread are seen by the other. And your if (flag == false)
and if (flag == true)
tests should actually be while
loops, because wait()
can wake up spuriously when notify()
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 Token
class is, but nothing in your queue actually depends on its type. It'd make more sense to define a generic PQueue<T>
, and then use PQueue<Token>
if you want to pass tokens.
Upvotes: 3