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

📄 eventqueue.java

📁 用java写的jt-jpeg。jt表示java time package
💻 JAVA
字号:
/*
 |
 | EventQueue.java
 |
 | EventQueue class
 | James Shin Young
 |
 | Created:  January 8, 1998
 |
 | Copyright (c) 1998 by James Shin Young and the Regents
 | of the University of California.  All rights reserved.
 |
 | Permission to use, copy, modify, and distribute this software
 | and its documentation for NON-COMMERCIAL purposes and without
 | fee is hereby granted provided that this copyright notice
 | appears in all copies.
 |
 */

package jcp;

/**
 *  A fixed-size FIFO queue of Event objects.
 *  @author James Shin Young
 */

public class EventQueue
{

    // Array of Events in queue. The queue is stored as a circular buffer.
    private Event[] events;

    // The index of the first element in the queue.
    private int first;

    // The index of the last element in the queue.
    private int last;

    /**
     *  @param num  The number of events the queue can hold.
     */
    public EventQueue(int num) {
        events = new Event[num+1];

        // Queue is empty when created, of course.
        first = 0;
        last = 0;
    }

    /**
     *  Returns the number of events in the queue.
     */
    public int length() {
        if (last >= first) {
            return (last - first);
        } else {
            return (last - first + events.length);
        }
    }

    /**
     *  Add the given Event to the end of the queue.
     *  @return true    if the Event was added successfully, false if the
     *                  event queue is currently full
     *  @param Event The Event to be added.
     */
    public synchronized boolean add(Event event) {

        // If queue is full, return false
        if (full()) {
            return false;

        // If queue is not full, add the event to the queue.
        } else {
            events[last] = event;
            last = increment(last);

            // Notify get method that might be waiting.
            this.notify();

            return true;
        }
    }

    /**
     *  Gets and removes the next Event in the queue.
     *  @return The next event in the FIFO queue
     */
    public synchronized Event get() {

        boolean success = false;
        Event event = null;

        while (!success) {
            try {
                if (empty()) {
                    this.wait();
                } else {
                    event = events[first];
                    success = true;
                }
            } catch (InterruptedException e) {}
        }

        // Remove the element from the queue.
        first = increment(first);

        return event;
    }

    /**
     *  Erases contents of queue.
     */
    public synchronized void flush() {
        first = 0;
        last = 0;
    }

    /**
     *  Increases the size of the queue by the specified amount.
     */
    public synchronized void increaseSize(int increment) {
        Event[] newQ = new Event[events.length + increment];

        for (int i=0; i < events.length; i++) {
            newQ[i] = events[i];
        }

        events = newQ;
    }

    /*
     *  @return true if the circular buffer is full
     */
    public boolean full() {
        return (length()+1 >= events.length);
    }

    /*
     *  @return true if the circular buffer is empty
     */
    public boolean empty() {
        return (first == last);
    }

    /**
     *  Returns the capacity of the event queue.
     */
    public int capacity() {
        return events.length-1;
    }

    /*
     *  Increments the indices used to keep track of the
     *  beginning and end of the queue.
     */
    private int increment(int val) {
        return ((val+1)%events.length);
    }
}

⌨️ 快捷键说明

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