Results 1 to 4 of 4
-
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:
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:private volatile boolean reset = false; public synchronized void setReset(boolean reset) { this.reset = reset; }
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?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); }
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!
- 05-22-2012, 03:16 AM #2
Re: Synchronization of a variable during polling
Can you post a SSCCE? I need something to get my teeth into.
If you don't understand my response, don't ignore it, ask a question.
-
Re: Synchronization of a variable during polling
- 05-22-2012, 07:24 AM #4
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.)Get in the habit of using standard Java naming conventions!
Similar Threads
-
Need help with synchronization
By boblettoj99 in forum Threads and SynchronizationReplies: 1Last Post: 05-08-2012, 03:22 AM -
Polling mouse status directly without MouseEvent?
By Toll in forum AWT / SwingReplies: 4Last Post: 06-05-2011, 02:16 PM -
Tail without polling
By taotree in forum Advanced JavaReplies: 2Last Post: 11-12-2008, 08:57 AM -
FrameWork polling
By nanaji in forum Advanced JavaReplies: 0Last Post: 10-29-2008, 10:16 AM -
Synchronization problems
By Jack in forum Advanced JavaReplies: 2Last Post: 07-02-2007, 01:17 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks