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

📄 synchautonomouscomponent.java

📁 用java写的jt-jpeg。jt表示java time package
💻 JAVA
字号:
/*
 |
 | SynchAutonomousComponent.java
 |
 | SynchAutonomousComponent 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;

/**
 *  Autonomous components contain an independent thread of execution, and
 *  thus are not limited to reacting to external stimuli. The behavior of the
 *  thread is described by the run method, as with typical Java threads.
 *  Due to the autonomous nature, a single reaction may last indefinitely,
 *  and therefore they are treated as a type of asynchronous component.
 *  @author James Shin Young
 *  @see Component
 *  @see AsynchComponent
 */

public abstract class SynchAutonomousComponent extends SynchComponent
    implements Runnable
{

    private Thread myThread;

    // Indicates the thread's state: has it been started yet?
    private boolean started;

    // Indicates the component's state: is it currently enabled?
    private boolean enabled;

    public SynchAutonomousComponent() {
        myThread = new Thread(this);
        started = false;
        enabled = false;
    }

    public final void start() {
        myThread.start();
        started = true;
    }

    public final boolean started() { return started; }

    public void enable() {
        super.enable();
        enabled = true;
        if (!started) {
            start();
        } else {
            myThread.resume();
        }
    }

    public void disable() {
        super.disable();
        enabled = false;
        if (started) {
            myThread.suspend();
        }
    }

    /**
     *  If the component has been started, stops the current invocation
     *  of the go() method, and begins a fresh invocation.
     */
    public void reset() {
        super.reset();

        if (started) {
            myThread.stop();
            myThread = new Thread(this);
            started = false;
        }

        if (enabled) {
            start();
        }
    }

    /**
     *  The method that implements the behavior of the component's
     *  thread.
     */
    public abstract void run();

    /**
     *  Invoked when a signal arrives on an active port. Does nothing by
     *  default.
     */
    public void go(Port p) { return; }

    /**
     *  Is this component's behavior alive?
     */
    public boolean isAlive() { return myThread.isAlive(); }
}

⌨️ 快捷键说明

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