I was hoping to get some feedback on what the recommended design pattern are for this subject.
public class Channel {
private AtomicInteger buffer = new AtomicInteger(0);
public void read() {
// This function should keep blocking until there is something to read.
while (buffer.get() == 0) {
// Maybe a sleep? (Although NetBeans gives a warning for a sleep in a loop)
}
buffer.decrementAndGet();
}
public void write() {
// This function should just write (nothing) to the buffer (by increasing the counter of the imaginary buffer).
buffer.incrementAndGet();
}
}
Is this safe for use in multithreading? Or is there a better approach?