+/EE360P

distributed systems

git clone https://git.julia.blue/+/EE360P

vijaygarg1add stack and queuecccc41d

master
529 B20 linesraw
1public class SingleQueue { // Single Producer Single Consumer
2  int head = 0;   // slot for get
3  int tail = 0;   // empty slot  for put
4  Object [] items; 
5  public SingleQueue(int size) {
6    head = 0; tail = 0;
7    items = new Object[size];
8  }
9  public void put(Object x) {
10    while (tail - head == items.length) {}; //busywait
11    items[tail % items.length] = x;
12    tail++;
13  }
14  public Object get() {
15    while (tail - head == 0) {}; // busywait
16    Object x = items[head % items.length];
17    head++;
18    return x;
19  }
20}