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

📄 asynchcomponent.java

📁 用java写的jt-jpeg。jt表示java time package
💻 JAVA
字号:
/*
 |
 | AsynchComponent.java
 |
 | AsynchComponent class
 | James Shin Young
 |
 | Created:  February, 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;
import java.util.Vector;

/**
 *  An implementation of Component that executes its behavior in
 *  an independent thread. Incoming signals are placed in an event
 *  queue and processed sequentially by the thread.
 *
 *  @author James Shin Young
 */

public abstract class AsynchComponent extends Component
{

    public static final int defaultQueueSize = 1;

    // Incoming event queue
    private EventQueue eventQueue;

    // Is this component enabled or not?
    private boolean enabled;

    // The thread that performs the component's behavior
    private SlaveThread myThread;

///////////////////////////////////////////////////////////////////////////////
//* Constructors

    /**
     *  Create a new asynchronous component.
     *  @param queueSize    The capacity of the component's internal event queue.
     *  A too-small value for this will result in LostObjectExceptions being
     *  thrown.
     */
    public AsynchComponent(int queueSize) {

        super();

        // Create the input event FIFO queue.
        eventQueue = new EventQueue(queueSize);

        // Create and start this component's thread.
        myThread = new SlaveThread(this);
        myThread.start();

        enable();

    }

    /**
     *  Create a new asynchronous component with a single-entry queue.
     */
    public AsynchComponent() {
        this(defaultQueueSize);
    }

    public void enable() {
        super.enable();
        enabled = true;
    }

    public void disable() {
        super.disable();
        enabled = false;
    }

    public void reset() {
        super.reset();
        if (eventQueue != null) {
            eventQueue.flush();
        }
    }

    /**
     *  This method is called by the ports whenever a new signal is received.
     *  It adds an event to the component's event queue and returns. The
     *  event queue is then processed by a separate thread dedicated to
     *  that task.
     */
    protected final synchronized boolean activate(Port port, Object signal) {

        if (enabled) {
            if (!eventQueue.add(new Event(signal,port))) {
                JTSystem.error("signal: "+signal.toString()+
                               " lost by "+this.toString());
                return false;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }

    // This method is executed by the component's SlaveThread and is
    // responsible for invoking the go() method.
    protected final void compRun() {
        while(true) {
            Event event = eventQueue.get();
            Port port = event.port();
            Object next = event.signal();
            Object previous = port.signal();

            // Execute the go method only on signal *changes*.
            if (!previous.equals(next)) {
                port.updateSignal(next);
                this.go(port);
            }
        }
    }

///////////////////////////////////////////////////////////////////////////////
//* Inner classes

    // Declaration of the thread object that executes the behavior of the
    // component. It is implemented indirectly using an inner class so that
    // subclasses of Component can define their own run() methods without
    // interfering with this behavior.
    private class SlaveThread extends Thread {

        private AsynchComponent owner;

        SlaveThread(AsynchComponent owner) {
            this.owner = owner;
        }

        public final void run() {
            owner.compRun();
        }
    }
}

⌨️ 快捷键说明

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