Multithreading and Stuff

edited December 2013 in How To...

Hi, I am new to this forum and to the Processing language. I am trying to become better at the language, because I see a great future in it.

Right now I am kind of testing out the limits of the language, I suppose. Here is what I want todo:

I want to make an interactable simulation of a neuron-network, where the user is able to place, move and set the behavior of single (2D/3D) neurons in real time. Each neuron would be represented by a class which holds all the necessary information about itself and its environment. In order to avoid arrays that are too long, which could possible clog up the memory and performance, I thought of possibly inventing some sort of scheduler that would provide each entity with a set of functions to demand execution at certain points in time in order to make the needed changes, such as checking the states of connected neurons e.t.c., but also to render/redraw itself independently.

Am I asking for too much here? It seems like things such as multithreading are no official feature of Processing and I have already experienced some bugs with it, too. Should I drop this whole idea?

Answers

  • edited December 2013

    In order to avoid arrays that are too long, which could possible clog up the memory and performance,...

    The arrays themselves don't take much room. It's about 4 bytes for each entry (8 bytes instead for 64-bit app).
    In other words, those arrays merely store the references for the objects.

    However, the object's fields are the 1s which actually count on determining how much RAM's gonna be used!
    Another thing to take in consideration is how many of the objects need to be displayed at the same time!

    It seems like things such as multithreading are no official feature of Processing...

    Processing itself is just a framework + collection of functions (API).
    Processing needs another programming language in order to compile and run!
    Java is the official language. But JavaScript is very well supported as well! And there are others!

    Even though it's not officially canonized, all the power of Java's multithreading is available for us to use! (*)

  • Thanks for taking the time, GoToLoop.

    I described my array-problem poorly. Basically what I meant was, only some units would need execution at a time, letting a main thread walk an array of possibly thousands of members and performing checks whether each unit would need execution would possibly waste a lot of CPU. Hence the idea of the objects scheduling themselves.

    If multithreading really is no bad idea, I would like to ask about this bug (or at least I assume it's a bug) I experienced while trying out the thread object. What I did was make a test-class that would run its own thread in a loop, each cycle it was supposed to change the position of a ellipse. At the end the class was supposed to represent an orbiting ellipse. This is sort of what its looking like now:

    class Spinner extends Thread{
    
      boolean is_active;
      int x,y,speed,t;
    
      Spinner(int x, int y, int speed)
      {
         this.t=40;
         this.x=x;
         this.y=y;
         this.speed=speed;
         this.is_active=false;
      }
    
      void run(){
        this.is_active=true;
        fill(190);
        while(this.is_active){
          t++; 
          //background(222); //erase previous ellipse
          ellipse(this.x+(sin(t)*40),this.y+(sin(t)*40),80,80);
          print("loopy "+t+" x:"+(this.x+(sin(t)*20))+" y:"+(this.x+(sin(t)*40))+"\n");
          try{ this.sleep(100); }catch(Exception e){} 
        }
      }
    
    
    }
    

    So when I run this, depending on how short the sleep interval is it draws a few ellipse shapes, but then stops for some reason even tho the thread is still running. Is this because PShape isn't thread-safe? This seems to be a problem related with the sleep workaround.

    Oh and: I run this on (64bit) Ubuntu 12.04 LTS with JDK 1.7.0.

  • edited December 2013 Answer ✓

    Is this because PShape isn't thread-safe?

    I don't see any PShape in your code above!? @-)

    ... but then stops for some reason even tho the thread is still running.

    You shouldn't have more than 1 Thread drawing to the main PGraphics object referenced by field g! :-w
    Create a separate PGraphics for your Thread instance w/ createGraphics()!

    Now, even doing so, multithreading doesn't go too well w/ drawing! It is best for other task types! -)

Sign In or Register to comment.