Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By Norm
  • 1 Post By kjkrum

Thread: Synchronization of a variable during polling

  1. #1
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Synchronization of a variable during polling

    I need help with concurrency, one of my weaknesses.

    I have a program that is continually polling the screen to see if a program I am trying to control has changed signficantly. It compares the current image to a reference image. There are times when I want to change the reference image to the current image, and so I have given the polling class a volatile private boolean called reset plus a synchronized setter method:

    Java Code:
       private volatile boolean reset = false;
    
    
       public synchronized void setReset(boolean reset) {
          this.reset = reset;
       }
    I don't want an outside class calling setReset when the image is being reset, so in a bit of code that is being called repeatedly, I have a synchronized block:

    Java Code:
       public void startPolling() {
          final Runnable myRunnable = new Runnable() {
             public void run() {
    
                // bunch of code deleted
    
                // but this is where I compare the currently obtained image
                // with the reference image
    
                synchronized (this) {
                   if (reset) {
                      referenceImgDataVector = currentImgDataVector;
                      reset = false; // should I call the setter here?
                      // setReset(false); // like so?
                   }
                }
    
             }
          };
          long initialDelay = 0;
          long period = freq;
          TimeUnit timeUnit = TimeUnit.MILLISECONDS;
          scheduler = Executors.newScheduledThreadPool(1);
          futureHandler = scheduler.scheduleAtFixedRate(myRunnable, initialDelay,
                period, timeUnit);
       }
    There will only be one instance of the above class, and there will only be one outside object ever calling reset. Will this work for me?
    And should I call the setter in the synchronized block?

    If an SSCCE is desired, I'd be happy to make one.

    Thanks in advance!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default Re: Synchronization of a variable during polling

    Can you post a SSCCE? I need something to get my teeth into.
    Fubarable likes this.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Synchronization of a variable during polling

    Quote Originally Posted by Norm View Post
    Can you post a SSCCE? I need something to get my teeth into.
    Thanks Norm. I'm going to try to produce one tomorrow.

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Synchronization of a variable during polling

    One problem I can see: synchronized(this) in the Runnable synchronizes on the anonymous Runnable instance, not the enclosing class instance. It should be synchronized(EnclosingClassName.this).

    If the reset variable in the enclosing class is not hidden by a local variable or member of the Runnable, then once you fix the synchronization you can just put reset = false in the synchronized block. (If it is hidden, you could put EnclosingClassName.this.reset = false.)
    Fubarable likes this.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Need help with synchronization
    By boblettoj99 in forum Threads and Synchronization
    Replies: 1
    Last Post: 05-08-2012, 04:22 AM
  2. Replies: 4
    Last Post: 06-05-2011, 03:16 PM
  3. Tail without polling
    By taotree in forum Advanced Java
    Replies: 2
    Last Post: 11-12-2008, 09:57 AM
  4. FrameWork polling
    By nanaji in forum Advanced Java
    Replies: 0
    Last Post: 10-29-2008, 11:16 AM
  5. Synchronization problems
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 02:17 AM

Posting Permissions

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