⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectfifo.java

📁 彩信网关程序,SP程序,包含移动彩信代码和电信彩信代码!供学习之用
💻 JAVA
字号:
package com.hxyh.sanny.mms.cmcc.base.thread;


public class ObjectFIFO
{

    private Object queue[];
    private int capacity;
    private int size;
    private int head;
    private int tail;

    public ObjectFIFO(int cap)
    {
        capacity = cap <= 0 ? 1 : cap;
        queue = new Object[capacity];
        head = 0;
        tail = 0;
        size = 0;
    }

    public int getCapacity()
    {
        return capacity;
    }

    public synchronized int getSize()
    {
        return size;
    }

    public synchronized boolean isEmpty()
    {
        return size == 0;
    }

    public synchronized boolean isFull()
    {
        return size == capacity;
    }

    public synchronized void add(Object obj)
        throws InterruptedException
    {
        waitWhileFull();
        queue[head] = obj;
        head = (head + 1) % capacity;
        size++;
        notifyAll();
    }

    public synchronized void addEach(Object list[])
        throws InterruptedException
    {
        for(int i = 0; i < list.length; i++)
            add(list[i]);

    }

    public synchronized Object remove()
        throws InterruptedException
    {
        waitWhileEmpty();
        Object obj = queue[tail];
        queue[tail] = null;
        tail = (tail + 1) % capacity;
        size--;
        notifyAll();
        return obj;
    }

    public synchronized Object[] removeAll()
        throws InterruptedException
    {
        Object list[] = new Object[size];
        for(int i = 0; i < list.length; i++)
            list[i] = remove();

        return list;
    }

    public synchronized Object[] removeAtLeastOne()
        throws InterruptedException
    {
        waitWhileEmpty();
        return removeAll();
    }

    public synchronized boolean waitUntilEmpty(long msTimeout)
        throws InterruptedException
    {
        if(msTimeout == 0L)
        {
            waitUntilEmpty();
            return true;
        }
        long endTime = System.currentTimeMillis() + msTimeout;
        for(long msRemaining = msTimeout; !isEmpty() && msRemaining > 0L; msRemaining = endTime - System.currentTimeMillis())
            wait(msRemaining);

        return isEmpty();
    }

    public synchronized void waitUntilEmpty()
        throws InterruptedException
    {
        for(; !isEmpty(); wait());
    }

    public synchronized void waitWhileEmpty()
        throws InterruptedException
    {
        for(; isEmpty(); wait());
    }

    public synchronized void waitUntilFull()
        throws InterruptedException
    {
        for(; !isFull(); wait());
    }

    public synchronized void waitWhileFull()
        throws InterruptedException
    {
        for(; isFull(); wait());
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -