Synchornization of Serial Connections In Java

I have developed software to send and recieve sms via a serial connection using AT commands and the Java Comm api, i have now come across the problem of synchronization i have achieved the following:

-Read in sms from connected phone every 2 seconds
-Sends SMS via the connected phone,

the problem i have is as the read thread is using the serial connection, when a request for sending an sms occurs i want to halt the read thread and start the write thread when completed i want to continue with the read thread, therefore using shared resources to give an idea of what i have done:

this is my read thread

  public void run(){                              
       try {                                      
         while(!isDone) {            
          //if(isDone)      break;    
      } catch(Exception e) {

Any help or advice will be greatly appreciated
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The problem here is that one thread can't tell another thread to wait. You would need to call interrupt() on the read thread and have it catch the resulting exception. It would then have to call wait() itself.

You would also need to have the write thread wait until the read thread was about to wait before it attempted to continue, so it starts to get quite messy and sounds like a recipe for deadlock.

What is the actual problem here? Is it that you can't write to the serial connection while another thread is reading from it or that you want the "write" thread to read the response from the AT+CMGS command and not have it compete with the read thread?

If the latter, it might be better to have a flag somewhere that says "waiting for response" and have the reader check this flag when it gets input. The writer can set this flag, send the AT command and then call wait(int timeout). When the reader get its input and checks the flag, it can store the response somewhere, reset the flag and call notify().

Note that in general you will call wait() and notify() on a separate object, not on the threads themselves. Also, the calling thread must own that object's monitor (i.e. must be synchronized on the object). So for example:

static Object lock=new Object();

in writer...

  synchronized(lock) {

in reader...

  synchronized(lock) {
    if (sendingSMSFlag) {
    } else {

(you may want to revisit what exactly should be included in the various synchronized blocks, but hopefully that gives you the idea...)
ACEAFTYAuthor Commented:
The system is automated as it is a surveillance system, the system listens via the serial connection for new text messages, when it reads a new text message in it reads the body of the text for example "ping", this particular command checks the status of the surveillance system and returns the state of the system as a text message bak to the user so:

1. listen for text messages every 2 seconds
2.if message body is "ping"
3.halt listening for text messages
4. send a new text message
5. continue to listen for more text messages

both listening and sending messages use the same serial connection hence the requirement for syncrnization, i have over come the problem by using wait() and notify() once the message has been sent this seems to work however is this the best approache to this problem?
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

I would have thought CEHJ gave a pointer in the right direction and I gave a workable answer. Split points would seem fairer to me, failing further feedback from ACEAFTY.

One problem only -- it is clear that the Asker still have troubles and you both decided not to continue helping. Answering questions in EE does not mean to post something and go away.. it means to actually work with the Askers..

 Which means the Asker gets the points back.
I understand, however I didn't decide not to continue helping - my take on this was that the asker had been given a working solution (and confirmed this fact) but was asking if anyone had a better way. So I waited to see if anyone came up with one.

Honestly, if I thought there was a better way I would have suggested it in the first place, so I didn't think there was any point in repeating myself. Perhaps I should have replied with something like "That's the way I would have done it anyway"...?

I guess I'll keep this in mind in future!
OK -- this question is back to the desk.

please leave it open for a second recommendation. Thanks
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.