Java 为什么';当主方法完成时,我的程序没有完成吗?

Java 为什么';当主方法完成时,我的程序没有完成吗?,java,serial-port,arduino,event-listener,Java,Serial Port,Arduino,Event Listener,我最近得到了一个arduino,并开始玩它。我编写了一个java程序来监听来自arduino的串行端口的输入(或者我稍微修改了一些示例代码) 代码如下: public class SerialListener implements SerialPortEventListener { SerialPort serialPort; /** * A BufferedReader which will be fed by a InputStreamReader * converting the

我最近得到了一个
arduino
,并开始玩它。我编写了一个
java
程序来监听来自
arduino
的串行端口的输入(或者我稍微修改了一些示例代码)

代码如下:

public class SerialListener implements SerialPortEventListener {
SerialPort serialPort;

/**
 * A BufferedReader which will be fed by a InputStreamReader 
 * converting the bytes into characters 
 * making the displayed results codepage independent
 */
private BufferedReader input;
/** The output stream to the port */
private OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;

public void initialize() {
    // the next line is for Raspberry Pi and 
    // gets us into the while loop and was suggested here was suggested http://www.raspberrypi.org/phpBB3/viewtopic.php?f=81&t=32186
    // System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");

    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
    while (portEnum.hasMoreElements()) {
        CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
        if (currPortId.getName().equals("COM3")) {
            portId = currPortId;
            break;
        }
    }


    try {
        // open serial port, and use class name for the appName.
        serialPort = (SerialPort) portId.open(this.getClass().getName(),
                TIME_OUT);

        // set port parameters
        serialPort.setSerialPortParams(DATA_RATE,
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);

        // open the streams
        input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
        output = serialPort.getOutputStream();

        // add event listeners
        serialPort.addEventListener(this);
        serialPort.notifyOnDataAvailable(true);
    } catch (Exception e) {
        System.err.println(e.toString());
    }
}

/**
 * This should be called when you stop using the port.
 * This will prevent port locking on platforms like Linux.
 */
public synchronized void close() {
    if (serialPort != null) {
        serialPort.removeEventListener();
        serialPort.close();
    }
}

/**
 * Handle an event on the serial port. Read the data and print it.
 */
public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            String inputLine=input.readLine();
            System.out.println(inputLine);
            FileWriter fWriter = new FileWriter("C:\\Apache24\\htdocs\\temp.txt");
            BufferedWriter writer = new BufferedWriter (fWriter);
            writer.write(inputLine);
            writer.close();
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}

public static void main(String[] args) throws Exception {
    SerialListener tempListener = new SerialListener();
    tempListener.initialize();

}
}
我知道
SerialListener
类实现了处理
SerialPort事件的方法,并且我在initiate方法中设置了
SerialListner
类的对象以侦听事件

我不明白的是为什么这个程序没有立即退出,因为它在运行initialize方法后在main方法中什么都不做

事件侦听器是否在不同的线程上运行,或者为什么这样做?(因为这是我想要的)

我基本上复制的代码可以在这里找到:
在“Java示例代码”下,但正如您所看到的,我并没有启动一个类,使其保持1000秒的活动状态,就像他们在示例中所说的那样

tempListener.close();
从上的
SerialPort.addEventListener()

此侦听器接收的所有事件都由属于SerialPort对象的一个专用线程生成


因此,我猜
SerialPort
有一个线程正在运行。请稍后尝试调用
templastener.close();

您确定
templastener.initialize()吗
正在返回?是的,因为监听器确实设置好了?而且它确实在侦听事件。我猜SerialPort会启动一个非守护进程线程来完成它的工作,因为它会向监听器报告,而该线程仍在运行,从而防止JVM关闭。您应该检查它的文档,看看是否需要执行某些操作来关闭它把它干净利落地放下(或者让它的线程成为守护进程线程(后台线程)的一种方法).你知道我是如何使它成为一个守护线程的吗?我的想法是让这个程序一直在我的计算机上运行,并用我窗口外的当前温度更新我的网站谢谢!你是如何找到这些文档的?为什么SerialPort是一个GNU库,而这些文档都在oracle页面上?因为我快速地在google上搜索了一下,得到了r
javax.comm.SerialPort
相反?:PSo它不一样?:P我如何使用“that”SerialPort来代替?(我刚刚阅读了教程或Arduinos网页)我假设
gnu.io.SerialPort
做了类似的事情,因为事件处理程序生成单独的线程是有意义的(否则它实际上如何运行事件处理程序?)。