online compiler and debugger for c/c++

code. compile. run. debug. share.
Source Code   
Language
import java.util.LinkedList; import java.util.Queue; import java.util.Random; class BoundedBuffer { private final Queue<String> buffer; private final int capacity; public BoundedBuffer(int capacity) { this.buffer = new LinkedList<>(); this.capacity = capacity; } public synchronized void put(String message) throws InterruptedException { while (buffer.size() == capacity) { // Sleep until buffer is not full wait(); } buffer.add(message); notifyAll(); // Wake everyone up } public synchronized String get() throws InterruptedException { while (buffer.isEmpty()) { // Sleep untill buffer is not empty wait(); } String message = buffer.remove(); notifyAll(); return message; // Wake everyone up } } class Producer implements Runnable { private final BoundedBuffer buffer; private final int id; private final Random random = new Random(); Producer(BoundedBuffer buffer, int id) { this.buffer = buffer; this.id = id; } @Override public void run() { try { for (int i = 0; i < 10; i++) { String message = "Message " + i + " from Producer " + id; buffer.put(message); Thread.sleep(random.nextInt(1000)); } buffer.put("Bye"); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } class Consumer implements Runnable { private final BoundedBuffer buffer; private final int id; private final Random random = new Random(); Consumer(BoundedBuffer buffer, int id) { this.buffer = buffer; this.id = id; } @Override public void run() { try { while (true) { String message = buffer.get(); System.out.println("Consumer " + id + ": " + message); if ("Bye".equals(message)) { break; } Thread.sleep(random.nextInt(100)); } } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } public class BoundedBufferDemo { private static final int BUFFER_SIZE = 32; private static final int NUM_PRODUCERS = 2; private static final int NUM_CONSUMERS = 2; public static void main(String[] args) throws InterruptedException { BoundedBuffer buffer = new BoundedBuffer(BUFFER_SIZE); Thread[] producers = new Thread[NUM_PRODUCERS]; Thread[] consumers = new Thread[NUM_CONSUMERS]; for (int i = 0; i < NUM_PRODUCERS; i++) { producers[i] = new Thread(new Producer(buffer, i)); producers[i].start(); } for (int i = 0; i < NUM_CONSUMERS; i++) { consumers[i] = new Thread(new Consumer(buffer, i)); consumers[i].start(); } for (Thread producer : producers) { producer.join(); } for (Thread consumer : consumers) { consumer.join(); } } }

Compiling Program...

Command line arguments:
Standard Input: Interactive Console Text
×

                

                

Program is not being debugged. Click "Debug" button to start program in debug mode.

#FunctionFile:Line
VariableValue
RegisterValue
ExpressionValue