Niko,
If I read the serial port permanently, my cpu ussage is about 17%. I think it wouldn't be less if I put it in a thread and read the port permanently, isn't it?
No. It probably would not. Unfortunately, there is no real way around it, it your serial port is sending you junk, you either have to fix the hardware or have your program read it. The purpose of starting another thread is not to save CPU, but to make sure that the rest of your program get a fair share of it.
Can you tell me how i can get the result from another thread? Because when the thread is running I can't call other methods in the thread class and I think with wait and notify it doesn't work well. Sure I can declare a variable that is static so that it can be read by other threads, but then I don't know when there is a new result arrived.
I would suggest picking up a very good book on threading. (
Java Threads, from O'Reilly, has just been updated for the latest JDK in its third edition...
Data passing, and the synchronization of it, is not simply done without a basic understanding of what you are doing. As for sending the data when it arrives, you could use a "listener"
pattern in your program design. There is a sample of that as well in the book.
Henry