Back
Close

Computing with Data

elgeish
33.4K views
Previous: Deadlocks - Part II Next: The Producer-Consumer Problem - Part II

The Producer-Consumer Problem - Part I

Let's talk a look at an example of a bad implementation; first, let's examine our shared resource, which is a bounded queue in this case that's protected with busy waiting:

class Shared {
  static final int MAX_BUFFER_SIZE = 3;
  static Queue<String> buffer = new ArrayDeque<>();
  private static volatile boolean shouldWait = true;
  
  static void waitUntilNotified() {
    while (shouldWait);
    shouldWait = true;
  }
  
  static void notifyWaitingThread() {
    shouldWait = false;
  }
}
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants