Results 1 to 3 of 3
  1. #1
    shorawitz is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default Sync two separate threads

    I have a need to have my app both synchronously query/react to a serial device, while also listening on a socket for updated parameters coming from another host.

    I need to have the socket thread be able to restart the serial thread once it receives updated information, so the serial thread can reread the new config information received by the socket thread.

    My current attempts have a tendency (on occasion) to restart the serial thread before it completes its while loop and goes to sleep. This causes an incomplete response to data received by the serial thread when the restart is out of sync.

    How would you do it? I'm thinking maybe I need a method in the Serial thread that checks a parameter to see if it needs to reread the config, but I'm not sure if that's the most efficient way to accomplish what I need.

    Example of classes in use:
    Java Code:
    Serial Thread {
        read serialConfigInfo;  # configs are stored on the local fs
    
        while (true) {
            while (listen on serial device) {
                react to data received by serial device;
            }
    
            sleep;  # this is where I need to be when restart method is called by
        }
    
    
    Socket Thread {
        while (true) {
            while (listen on socket for new config data) {
                write new config data to local fs;
            }
    
            call method to restart Serial Thread;
    
            sleep;
        }
    }
    Last edited by shorawitz; 01-07-2011 at 03:18 PM.

  2. #2
    ClickerMonkey is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    Hmmm here's what I would do.
    Java Code:
    class SerialThread {
       final Object lock = new Object();
       volatile boolean validate = true;
       while (true) {
          synchronized (lock) {  
             if (validate) {
                read config;   
                validate = false;
             }
          }
          // if this is a blocking procedure the SocketThread should invoke
          // interrupt() on this thread after it sets validate to true. 
          try {
             while (listen on serial device [U]AND[/U] validate == false) {
                 react to data received by serial device;
             }
          }
          catch (InterruptedException ie) {
             // gobble gobble
          }
       }
    }
    
    class SocketThread {
       SerialThread serialThread;
       while (true) {
          waiting for data on socket;
          write new config data to local fs;
          synchronized (serialThread.lock) {
             serialThread.validate = true;
             // wakes serial thread from listening to device
             serialThread.interrupt();
          }
       }
    
    }
    I think that would do it, so technically you wouldn't "restart" the thread, just awake it.
    Last edited by ClickerMonkey; 01-13-2011 at 03:41 AM.

  3. #3
    shorawitz is offline Member
    Join Date
    Nov 2010
    Posts
    3
    Rep Power
    0

    Default thank you

    Thanks ClickerMonkey! I ended up doing something very similar.

Similar Threads

  1. file sync from FTP to FTP
    By kgk04 in forum Advanced Java
    Replies: 0
    Last Post: 12-18-2010, 10:15 AM
  2. thered sync
    By miko5054 in forum Threads and Synchronization
    Replies: 2
    Last Post: 05-25-2010, 10:51 AM
  3. vertical sync
    By afraidofdark in forum Java 2D
    Replies: 0
    Last Post: 03-25-2010, 08:54 PM
  4. Should I separate my code into separate files?
    By Inks in forum New To Java
    Replies: 0
    Last Post: 03-26-2009, 12:12 AM
  5. LOG4j Logging in Separate Files for Threads
    By krishna_85 in forum New To Java
    Replies: 0
    Last Post: 03-05-2009, 04:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •