devicestarter.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 111 行

JAVA
111
字号
/*
 * $Id: DeviceStarter.java,v 1.2 2004/02/26 10:33:48 epr Exp $
 */
package org.jnode.driver;

import org.jnode.system.BootLog;
import org.jnode.util.TimeoutException;
import org.jnode.work.Work;
import org.jnode.work.WorkUtils;

/**
 * @author Ewout Prangsma (epr@users.sourceforge.net)
 */
public final class DeviceStarter {

    final Device device;

    private long startTime;

    private long endTime;

    private DriverException exception;

    /**
     * Initialize this instance.
     * 
     * @param device
     */
    public DeviceStarter(Device device) {
        this.device = device;
    }

    /**
     * Start the device.
     */
    public final synchronized void start(long timeout) throws TimeoutException, DriverException {
        startTime = 0;
        endTime = 0;
        exception = null;
        WorkUtils.add(new StartWork());
        // Wait for the worker to start.
        int loops = 10;
        while ((startTime == 0) && (loops-- > 0)) {
            try {
                wait(timeout);
            } catch (InterruptedException ex) {
                // Ignore
            }
        }
        if (startTime == 0) {
            throw new TimeoutException("Device " + device.getId() + " is given no time to start");
        }
        // Wait for the work to finish
        long now = System.currentTimeMillis();
        while ((endTime == 0) && ((now-startTime) < timeout)) {
            try {
                wait(timeout / 3);
                BootLog.info("..waiting..");
            } catch (InterruptedException ex) {
                // Ignore
            }
            now = System.currentTimeMillis();
        }
        if (endTime == 0) {
            throw new TimeoutException("Device " + device.getId() + " did not start in time");
        }
        if (exception != null) {
            throw exception;
        }
    }

    /**
     * Notify the start to the worker.
     */
    synchronized final void notifyStart() {
        startTime = System.currentTimeMillis();
        notifyAll();
    }

    /**
     * Notify the start to the worker.
     */
    synchronized final void notifyEnd() {
        endTime = System.currentTimeMillis();
        notifyAll();
    }

    class StartWork extends Work {

        public StartWork() {
            super("Starter of " + device.getId());
        }

        /**
         * @see org.jnode.work.Work#execute()
         */
        public void execute() {
            notifyStart();
            try {
                try {
                    device.start();
                } catch (DriverException ex) {
                    exception = ex;
                }
            } finally {
                notifyEnd();
            }
        }
    }
}

⌨️ 快捷键说明

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