WorkStack.java 673 字节
Newer Older
武汉红喜's avatar
武汉红喜 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
package org.hongxi.java.util.concurrent.sync;

/**
 * @author shenhongxi 2019/8/11
 */
public class WorkStack {

    private int index = 0;
    private Work[] works = new Work[6];

    public synchronized void push(Work work) {
        while (index == works.length) {
            try {
                this.wait();
            } catch (InterruptedException e) {}
        }
        this.notifyAll();
        works[index++] = work;
    }

    public synchronized Work pop() {
        while (index == 0) {
            try {
                this.wait();
            } catch (InterruptedException e) {}
        }
        this.notifyAll();
        return works[--index];
    }
}