is this a multythreading app?
in
Programming Questions
•
9 months ago
i'm working on a multy threading app in processing, this is my first result, but is real multythreading or i can obtain something more?
- int threads = 2; //numero di threads da creare
int n = 10000;
particle particles[] = new particle[n];
worker process[] = new worker[threads]; //array di processi
Thread threadProcess[] = new Thread[threads]; //array di thread contenente process
void setup(){
background(0);
size(400,400);
background(0);
stroke(255);
smooth();
for (int i=0; i<n; i++){
particles[i] = new particle();
}
//scorri i thread, e di da dove a dove calcolare (start, stop);
for (int i=0, start = 0, stop = n/threads; //inizializza variabili
i<threads; //condizione d'uscita
i++, start+=stop){ //aggiorna variabili a ogni ciclo
process[i] = new worker();
process[i].start = start;
process[i].stop = stop;
stop += n/threads;
threadProcess[i] = new Thread (process[i]);
}
}
void draw(){
background(0);
for (int i=0; i<threads; i++){
process[i].run();
}
for (int i=0; i<n; i++){
particles[i].draw();
}
println(frameRate);
}
/*------------------------------*/
class worker implements Runnable{
int start, stop;
void run(){ //metodo obbligatorio
for (int i=start; i<stop || i<n; i++){//i<n evita di uscire dall'array particles[n]
particles[i].update();
}
}
}
/*------------------------------*/
class particle{
PVector pos = new PVector(random(width),random(height));
PVector vel = new PVector(random(-5,5), random(-5,5));
void update(){
if(pos.x>width || pos.x<0) vel.x*=-0.7;
if(pos.y>height || pos.y<0) vel.y*=-0.7;
pos.add(vel);
}
void draw(){
point(pos.x,pos.y);
}
}
1