We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpIntegration › Synchronize multiple threads and draw
Page Index Toggle Pages: 1
Synchronize multiple threads and draw (Read 1344 times)
Synchronize multiple threads and draw
Dec 11th, 2009, 9:19am
 
Hej,

I'm drawing a huge number of ellipses and rects saved as objects in an arraylist, what slows down the framerate.

Code:
public void draw(){       
       for (int i = 0; i < ellipses.size()-1;  i++) {
   Ellipses Circle = (Ellipses) ellipses.get(i);
   ellipse(Circle.getX(),Circle.getY(), 3, 3);}
       
       for (int i = 0; i < squares.size()-1;  i++) {
   Rect quad = (Rect) squares.get(i);
   rect(quad.getX(),quad.getY(), 3, 3);}
       ...}


Does anybody know wether there is a way to do the two "for loops" simultaneously?  

I tried some kind of threading. In Example: One thread draws an ellipse and the other thread a rect.

Code:

public class RectThread extends Thread {
 PApplet parent;

Code:
 
public RectThread (PApplet p){
parent = p;
...}


Everything in the "while loop" is repeated forever until the variable "running" is false or the thread is stopped what actually is the same.

Code:
public void run ()
   {
       while (running==true){      
               if (drawBoole==true){
                   parent.rect(100,100,10,10);
     drawBoole=false;
        }
       }
       System.out.println(id + " thread is done!");  
   }


The draw() manipulates drawBoole trying to synchronize

Code:
public void draw() {
               background(255);
               fill(0);
thread1.drawBoole=true;
thread2.drawBoole=true;                
               ...}


All in all its roughly works. I can see both rect and ellipse but with a lot of flickering background.

I hope, I explained it understandable and would be glad if someone has a solution or a thought-provoking impulse

Thank a lot, fejngold




Re: Synchronize multiple threads and draw
Reply #1 - Dec 11th, 2009, 12:11pm
 
> I'm drawing a huge number of ellipses and rects saved as objects in an arraylist, what slows down the framerate.

this isn't surprising. doing lots of work takes a lot of time.

(and array.get is fast enough, is only an array access)

and splitting it into two threads doesn't change the total amount of work to do each frame or the time it'll take. (actually, it might if you have multiple processors... can processing do that?)

you might want to move the definition of Ellipses Circle and the calculation of ellipses.size outside of the loop, have the bare minimum inside it*. it probably won't change anything but...

lower your expectations and you'll be less disappointed 8)

* ie:
int s = ellipses.size() - 1;
Ellipse circle;
for (int i = 0; i < s;  i++) {
     circle = (Ellipses) ellipses.get(i);
     ellipse(circle.getX(), circle.getY(), 3, 3);
}
Re: Synchronize multiple threads and draw
Reply #2 - Dec 11th, 2009, 12:16pm
 
and have you tried OpenGL? either changing just the renderer in size or going the whole hog and using display lists or VBOs. (assuming you have the hardware for it.)
Page Index Toggle Pages: 1