What keeps this thread alive?
I have this sun example of how to read data from a serial port http://developer.java.sun.com/developer/releases/javacomm/SimpleRead.java.
I'm not interested in the serial port stuff but I wonder what exactly keeps the program / the thread alive?
The entire code:
* @(#)SimpleRead.java 1.12 98/06/25 SMI
* Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
* Sun grants you ("Licensee") a non-exclusive, royalty free, license
* to use, modify and redistribute this software in source and binary
* code form, provided that i) this copyright notice and license appear
* on all copies of the software; and ii) Licensee does not utilize the
* software in a manner which is disparaging to Sun.
* This software is provided "AS IS," without a warranty of any kind.
* ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
* INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
* ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
* LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
* SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
* BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
* INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
* HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
* OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* This software is not designed or intended for use in on-line control
* of aircraft, air traffic, aircraft navigation or aircraft
* communications; or in the design, construction, operation or
* maintenance of any nuclear facility. Licensee represents and
* warrants that it will not use or redistribute the Software for such
* purposes.
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args)
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements())
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL)
// print port names to adjust the desired com port
System.out.println("Serial Port: " + portId.getName());
// if (portId.getName().equals("COM1")) {
// this is (hard coded) the port I want to monitor
if (portId.getName().equals("COM4")) {
SimpleRead reader = new SimpleRead();
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
break;
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}The work of the thread is located in the run() method. As long as statements from this method are executed the thread is alive, at least this is what I've read so far in thread HowTos and stuff.
But:
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}doesn't contain an endlos loop. It sleeps for a fixed amount of time and then seems to finish. The programm or a thread of the program is still running after these 20 seconds expired and keeps reading data.
I've also seen a version with 100 ms instead of 20000.
So now back to my question: Why is this working?
Why 20000?
Can somebody enlighten me please?
I was going to implement an endlos loop to keep such a port monitoring thread alive but since the jave gurus shipped the example this way I'm kinda puzzled.
Any help is highly appreciated.
Mat.
As far as I know the serialEvent() method is a callback. (I think the registered listeners are usually callback aren't they).
I wish I could rely on this kind of technique. I even read some Thread faqs and howtos over but I couldn't figure out why this should be reliable working.
Maybe its just because the opening of the port leads to another thread created. The one which monitors the port in the first place and calls the registered listeners. So the 20 seconds may just be a very safe way to let this thread do all its initial work.
But then ... why doing a seperate thread in SimpleRead?
Thank you so far for your input,
Mat.
Similar Messages
-
I'm trying to burn playlists onto a cd from itunes. I keep getting a "medium write error" message. What does this mean? I heard that this may mean that I should try burning at a slower speed. I tried this and that didn't help. I also heard that I may need to use a different brand of CD-R disc. I have not tried this yet. I also heard that it could be a software issue or disc drive issue. I recently updated my macbook pro software from tiger to snow leapord. Do you think this could have affected my itunes playlist burning capabilities? Thanks for the help.
A system upgrade should not have affected anything. When did you notice the loss in functionality?
Try different media. 2011 post on optical media - http://discussions.apple.com/thread.jspa?threadID=2754858&
Apple Portables: Resetting the System Management Controller (SMC) - http://support.apple.com/kb/HT1411
Realize that a MBP that came with Tiger is an old computer and optical drives do not last forever.
You could try a drive cleaning disc to remove dust. Don't be overly optimistic. I suspect it is an old drive issue. -
I can't use my macbook.It keeps on rebooting everytime I start it.What is the cause of this problem? What can I do to fix it?
Have you read for possible solutions over in the "More Like This" thread over here?----------------------->
Mac Pro
Are desktop comps
Please correct your system profile if the above is incorrect. -
Have a IPad air. I cannot sync all my music from my computer. It keeps saying the device has timed out??? What does this mean? Unfortunately I have an older Dell that's running windows XP. Help please. Not a computer savvy person
Device Manager which is in Control Panels
I strongly recommend AIDA64 to show everything, temps, fans, chips, drivers and versions
http://www.aida64.com/ Windows has better tools to show you every service and knick and cranny
https://discussions.apple.com/thread/2769294
http://realitypod.com/category/hacks-and-cracks/mac-hacks-and-cracks/
MacBook definitely uses Cirrus - probably iMac -
My iPod doesn't sync it keeps coming up with the message "The iPod 'iPod Nano' cannot be synced. An unknown error occurred (13019)." What does this mean and how can I fix it?
I am getting the same error message. Have you figured it out?
-
What happened to this thread? --> LabVIEW poll "Features you would like to see in LabVIEW!"
Warren Massey -
Unfortunately, we were not able to migrate the polls and topics to the new system when we launched. The good news is that we still have access to the data and are investigating our migration options available.
We will also focus on getting some new topics and polls out to the community in the next few weeks.
Thanks!
Molly K.
Web Support & Operations Manager
National Instruments -
Hi - I keep receiving the following error message on a song I purchased from itunes and am now trying to drag and drop to my iPad: "you must download items from icloud before you can copy then to iPad" what does this mean - please help! Thank you
Hi Poonam26c,
If you do not have a local copy of a song which you have purchased (i.e. it is available via iTunes in the Cloud), you may need to redownload it before you can transfer it to your device. You may find the following article helpful:
Apple Support: Downloading past purchases from the iTunes Store, App Store, and iBooks Store
http://support.apple.com/kb/HT2519
Regards,
- Brenden -
I am attempting to sync my phone with iTunes and I keep getting an error message 42001 so my phone can not sync with iTunes... What is this error message and what can I do to fix this problem and sync my phone to iTunes?
Dear Apple team,
I recently bought an iphone3GS from USA and jail broke it. Since I heard the there are lots of disadvantages I thought to unlock it officially through the original owner (in copy). He assisted me and confirmed that he unlocked it with AT& T (See the attachment). According to the instructions I restored the phone. But it is not activating with “Activation server is currently not available, please try again or try with iTunes”
When I try with iTunes , it says “you haven’t inserted a SIM” even though I insert the original AT&T SIM.
My current location is Sri Lanka ,so don’t have any apple support.
Please help me to be a good fan of apple. -
I keep receiving an error message when I try to Sync my I phone 4. It says Can't Sync 1450. What does this mean? I've had this phone for over a year with no problems.
Yep. Tried that. And just did it again.
Still no joy. -
I am trying to update itunes to 11.1.1.11 and keep getting message at the end ' error writing to file :c:\Config.Msi\301ded48.rbf.Verify that you have access to that directory'. What does this mean and how do I fix it?
I had this exact same problem just now, trying to upgrade to 10.7.
I even got that error msg when trying to UNINSTALL iTunes!
After hours of frustration trying everyone's different suggestions, I downloaded Revo Uninstaller http://www.cnet.com.au/apps/revo-uninstaller-10610875/ which solved the problem.
Use it to help uninstall iTunes and related software components in the following order and then restart your computer:
1. iTunes
2. QuickTime
3. Apple Software Update
4. Apple Mobile Device Support
5. Bonjour
6. Apple Application Support (iTunes 9 or later)
Then you can install iTunes from scratch.
Good luck -
I keep greeting this message trying to set up iPad mini
Apple ID could not be created because of a server error on screen after term and conditions are accepted. What does this mean and how do I fix fix it? Has anyone else had this problem?Antonio,
Show ... it worked ... you are the man! This hint of clicking the right mouse button on the prompt and run as Administrator was perfect.
Thank you very much -
I am trying to set up my email on my ipday, but I keep getting yahoo server is unavailable, please try again later. What does this mean?
I just discover
ed that all you need to do is change you yahoo password from your computer and then set it up on your iphone. That's it -
I'm trying to download my Master CS6 onto a new hard drive because my old one crashed and I keep getting a message saying " Cannot write the file" or "I don't have permission to access the requested resource." ? What does this mean?
Yes- it is a Mac. I went through adobes system requirements and re- downloaded supportive software. The notice seems to happen near the end of the down load with the messages above. I'll look into your suggestion. Thank you- appreciated.
-
Im having trouble setting up my mail. I have just upgraded from a iphone 4 to 5. mail keeps asking for my password when i put it in it comes up saying
the operation could not be completed (MFMessageErrorDomain error 1032)
what is this??Nice try! Take a hike.
-
Android.process.acore? What is this and why does it keep not working?
Help: I've had my Lucid 3 phone since May and this phone has been the most confusing thing to operate. I'm currently having three (3) issues:
The Wi-Fi is not working...I keep getting the symbol refreshing. I can't turn the Wi-Fi on or off. I read "Turning Wi-Fi on..." when I go to the setting for Wi-Fi but this has been going on for 3 hours. C'mon! My Wi-Fi at home is working great; that's how I'm writing this post. Help!
To add to the misery: I keep getting a pop-up notification that reads: "Unfortunately, the android.process.acore has stopped." What is this and why does it not work. The phone's been getting this message for a week now...help!
Last concern: I'm still not getting multimedia texts (particularly pictures). The data and Wi-Fi were on...well, that's when the Wi-Fi was working (please refer to complaint #1 above). What the heck?
This phone is terrible news and I can't wait to replace it. In the meantime, the phone needs to work. Help.
-SaloWe certainly want to clear up all the confusion and have a working phone in your hands, srodezno. Let's take this one issue at a time:
1. How long ago did this trouble with WiFi start? Are you able to connect to any alternate WiFi signals other than the one at home?
2. Do you get that notification when you access anything in particular? Did it begin when you updated or installed any application?
3. Are you still able to access other data services (web, apps, Play Store, etc) while the WiFi is off/not working? Are you able to send the MMS messages?
JenniferH_VZW
Follow us on Twitter www.twitter.com/vzwsupport
Maybe you are looking for
-
Error while installing Database 10g on Linux Enterprise 5
Hi All, When I tried to install Database 10g this error apears All installer requirements met. Preparing to launch Oracle Universal Installer from /tmp/OraInstall2009-02-05_12-55-24PM. Please wait ...bash-3.2$ Exception in thread "main" java.lang.Uns
-
Setting flag account assignment element in PS within BADI doesn't work
Hello, I would like to set the flag for account assignement element and billing element in PS depending on some fields of cProjects. Hint 993642 says that can be done in GCC_PS_PROJECT_LABEL. Unfortunately I don't know how to pass some parameters fro
-
IOS 5 And IOS 5.0.1
What are the differences between ios 5 and ios 5.0.1 ? Thanks
-
Which is better to use Shell Script or Alerts in OMS
HI This is a point of confusion for me to decide that for monitoring the databases what should be used the Alerts Set in the Management Server or Shell scripts . May I have your valuable suggestions with reason for supporting the suggestion
-
Firefox "stops working" and shuts down every few mins or less.
A message pops up that says Firefox has stopped working and to close the program. I can "refresh" to the same spot but it's happening every few mins or less. Any suggestions? It's only been happening for the past 3 days, and like others who's systems