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

📄 synchronous.java

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

/**
 *  Synchronous objects are components that react only to a
 *  prespecified "clock" input. The go() method is not executed
 *  until the clock input is received.
 * @author James Shin Young
 */

public abstract class Synchronous extends SynchAutonomousComponent
{

    // Default size of internal event queue.
    private static int DEFAULT_QUEUE_SIZE = 4;

    // Maximum size of internal event queue.
    private static int MAX_QUEUE_SIZE = 64;

    // The number of slots by which to increase the event queue size when
    // needed.
    private static int QUEUE_INCREMENT = 2;

    // The clock inport
    private Port clockPort;

    // A generic object used to synchronize the user thread
    private Object lock;

    // Is the user thread waiting?
    private boolean waitState;

    // Event queue
    private EventQueue queue;

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

    public Synchronous() {

        lock = new Object();
        waitState = false;

        queue = new EventQueue(DEFAULT_QUEUE_SIZE);
        clockPort = super.addPort(true,"clk");
    }

    public final void go(Port port) {

        // Activate only on the clock inport
        if (port == clockPort) {
            // clock == true -> compute
            if (port.signal() == Signal.TRUE) {
                // Start the autonomous activity if not started yet..
                if (!started()) {
                    start();
                }
                goUntilWait();
            } else if (port.signal() == Signal.FALSE) {
            // clock == false -> update output
                updateOutputs();
            }
        }
    }

    /**
     *  Emits a signal to a port.
     *  @param sig  The signal to be sent.
     *  @param port The destination port.
     *  @return true if signal successfully emitted.
     */
    public boolean emit(Object sig, Port port) {
        // Instead of emitting the signal immediately to the port,
        // add a event to the queue

        if (queue.full()) {
            if ((queue.capacity() - QUEUE_INCREMENT) < MAX_QUEUE_SIZE) {
                JTSystem.println("Increased queue size on synchronous component "+
                                  this.toString());
                queue.increaseSize(QUEUE_INCREMENT);
            } else {
                JTSystem.error("Event queue on synchronous component: "+
                                this.toString()+
                                " reached maximum.");
            }
        }

        return queue.add(new Event(sig,port));

    }

    protected void waitClock() {
        synchronized (lock) {
            waitState = true;

            // Loop exits when clock thread signals that the
            // user thread can continue.
            while (waitState == true) {
                try {
                    // Wake up the clock thread
                    lock.notify();
                    lock.wait();
                } catch (InterruptedException e) {}
            }
        }
    }

    private void goUntilWait() {
        synchronized (lock) {
            // Reset waitState to indicate that user thread can
            // continue
            waitState = false;

            // Loop exits when user thread sets waitState to be
            // true
            while (waitState == false) {
                try {
                    // Wake up the thread executing the user behavior
                    lock.notify();
                    lock.wait();
                } catch (InterruptedException e) {}
            }
        }
    }

    private void updateOutputs() {
        // Process the events stored in the queue.
        while(!queue.empty()) {
            Event e = queue.get();
            super.emit(e.signal(),e.port());
        }
    }
}

⌨️ 快捷键说明

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