Strange flickering

I'm hoping someone has had experience with this and can help: I have a pretty elaborate sketch going, where I have two windows (with graphic output), each using data from the same array of a custom class. I have the second window going using PApplet. The problem is that the graphics in the second window flickers every now and then. The graphics in the first window (directly in my draw method) is running smoothly. Any ideas?

Tagged:

Answers

  • edited May 2018

    Difficult without seeing / trying the actual code but in the second window add the synchronized modifier to the draw method e.g.

    synchronized public void draw(){
    
    }
    

    No promises ;I

  • Thanks quark - never knew about synchronized... interesting! Unfortunately the flickering is still there. I noticed that if I make a second array inside the second window's draw method (essentially duplicating an array inside the primary draw method), the flickering is no longer there. Any further thoughts? Sorry I can't give much details, but there's a ton of code, and I'm sure some needs cleaning (and that could very well be the problem!).

  • The problem is almost certainly due to sharing the same data.

    Each window has its own event-processing-thread which is responsible for running its own draw method. These threads run asynchronously (i.e. independently). so the draw method can be interrupted at any time to process the other thread including its draw method.

    So if you are updating shared data during the main-window draw method this could be interrupted mid-update to draw in the second window. Of course the second window is now working with partially updated data so flickers.

    This would explain why having a second copy of the array does not flicker.

    There are a number of techniques you could use including semaphores and locks but since you are not getting concurrent access exceptions you might be able to get away with a simple boolean semaphore.

    Create a boolean variable e.g. data_locked which is set to true when you start updating the array and set to false when the update is complete.

    Since you are updating the data in the first window's draw method then you only have to test the flag in the second e.g.

    public void draw(){
      if(data_locked){
        return; // skip this render
      }
      // your code here
    }
    

    If this doesn't work you will need to try a different technique.

  • wow, that's super helpful! I'm sure it's impossible to say for sure without seeing the data, but on principle, which would you say is more process-efficient (creating a duplicate array (about 1,500 rows, 3 columns), or using this boolean check)?

  • Answer ✓

    See if the boolean semaphore works first!

    In principle it will be a more efficient than duplicating a 2D array. It also depends on whether the array is of primitives or objects, and if objects whether you are doing a shallow or deep copy.

  • the boolean semaphore strategy works like a charm, thanks so much! Thanks as well GoToLoop (as always) - using ArrayList may be more 'proper' - I may not to rewrite the code once I'm happy with the applet.

Sign In or Register to comment.