title: How to set timeout in Java when reading from SerialPort?
body: <p>I'm new to JAVA programming. I was assigned with the task to write a JAVA GUI application that will interact with external devices via serial port. I've managed to make my code compile and run with jSSC (Java Simple Serial Connector) package under both Mac OS X and Win32 platform. The jSSC project provides decent sample codes and it's fairly easy to implement with javax.comm style SerialPortEventListener interface.</p>
  
  <p>But soon I've learned that I'd need to add "ACK timeout" feature to my implementation, for error prevention. In C language this can easily be achieved via select() API call with 5th parameter set to desired duration dynamically. But in JAVA, I've no single clue how to do it.</p>
  
  <p>Should I setup another time thread and fire timer-generated event by directly calling serialEvent() method ? It may lead to some racing condition though.</p>
  
  <hr>
  
  <p>(03/27 10:43 Edit): OK, I've traced source code of jSSC. It seemed that jSSC just creates a new Runnable thread in JVM, when a Listener is registered to it; and that thread will continuously polling status with native function call, which will invoke system api calls like ioctl(). So it would be impossible to add timeout event without modifying the native library.</p>
  
  <p>Guess that I have to change the way for serial port event handling. I'll need to create another Runnable thread to handle the incoming data process, instead of directly parsing data within serialEvent() method call from jSSC event thread. And I can setup a timer to throw timeout event into that thread.</p>
  
  <p>I that case, I'd need to setup an event dispatching thread, just like SwingUtility.invokelater(). But I've no idea whether I should implement one from the bottom-up, or I can just use the AWT EDT to handle it.</p>
  
  <p>Can anyone give me some advice ?</p>