package com.example.domain;

import java.util.ArrayList;
import java.util.List;

public class SyncStack {

	private List<Character> buffer = new ArrayList<>();

	public synchronized char pop() {
		char c;
		while (buffer.size() == 0) {
			// if buffer is empty, wait
			try {
				this.wait();
			} catch (InterruptedException e) {
				// do nothing
			}
		}
		// if buffer is not empty, read char and return it
		c = buffer.remove(buffer.size() - 1);
		return c;
	}
	
	public synchronized void push(char c) {
		buffer.add(c);
		this.notify();  // after add char, notify waiting thread
	}
}
