Question about the correct usage of the thread() method and it's application to my code

Hi, I am rather new to processing and I was trying out the thread() method to speed up my code and run multiple instances of different sorting algorithms I’ve been developing lately, please note that I have just a basic understanding of threads.

My idea would be that I run the heaviest portions (sorting algorithms) via thread and have on draw() basically only a few lines of code that show the time passing and visualizing the state of the array I am sorting.

The problem is that if I, for instance, sort an array holding 100, 1000 or 10 000 the timer will always show the same time since the draw() methods “lags” which I thought wouldn’t be the case with threads

Here is the code, I included the “Sorter” and “Blubble” classes so that you may try the code yourself, but I’d immagine the problem is most likely nested in the draw(), Sorter is an abstract class which Bubble inherits from

I use threads in this program in the followign manner: I have a tread() in the setup() which executes “avanza”, a method of the class Bubble that basically cycles through the array once, that continues until the array is sorter, at which point sorter.get(0).fine equals to true.

to replicate the “error” just edit the followin line: sorter.add(new Bubble(1000,true)); at line 8 with increasingly bigger numbers.


ArrayList<Bubble>sorter=new ArrayList<Bubble>();
int frame=0;
void setup() {
  size(1000, 1200);
  stroke(0);
  strokeWeight(1);
  fill(204, 102, 0);
  sorter.add(new Bubble(1000,true));
  thread("Thread");
}
void draw() {
  background(200);
  stroke(0);
  sorter.get(0).disegnaColonne();
  if(!sorter.get(0).fine)frame++;
  textSize(70);
  text(frame, 100, 101);
  sorter.get(0).pointer(sorter.get(0).pivot);
  println(sorter.get(sorter.size()-1).fine);
}
public void Thread(){
  do{
  for(Sorter s:sorter)
    if(!s.fine)s.Avanza();
  }while(!sorter.get(0).fine);
}
abstract class Sorter{
  int[] vettore;
  float xMax;
  float yMax;
  boolean fine=false;
  
  public Sorter(int size,boolean isGradual){
    vettore = new int[size];
    xMax=width*1.0/vettore.length;
    yMax=height*1.0/vettore.length;
     
    int numero;
    if (isGradual) //isGradual = true rende i numeri più "puliti" come 12345, senza ripetizioni ne salti
     for (int i=0; i<vettore.length; i++) 
      do {
        numero=(int)random(vettore.length);
        if (vettore[numero]==0)vettore[numero]=i;
      } while (vettore[numero]!=i);
    else //altrimenti i numeri sono completamente random
    for (int i=0; i<vettore.length; i++) 
      vettore[i]=(int)random(vettore.length);
  }
  abstract void Avanza();
  
  void disegnaColonne() {
    fill(0, 0, 0);
    for (int x=0; x<vettore.length; x++)rect(x*xMax, height-yMax*vettore[x], xMax, yMax*vettore[x]);
  }

  void pointer(int val) {
    fill(255, 0, 0, 100);
    rect(val*xMax, 0, xMax, height);
  }
}
class Bubble extends Sorter{
int pivot;
int position= 0;

  public Bubble(int size,boolean isGradual) {
    super(size,isGradual); 
    pivot=vettore.length;
}

  void Avanza() {
  // se un intero cliclo non cambia posizione
  // allora il vettore è ordinato e clear=true
  boolean clear=false;
  if (!fine) 
    for (int i=1; i<pivot; i++)
      if (vettore[i-1]>vettore[i]) {
        int appoggio=vettore[i-1];
        vettore[i-1]=vettore[i];
        vettore[i]=appoggio;
        clear=false;
      }
  pivot--;
  fine=clear;
  if (pivot==0)fine=true; 
  }
}

Thanks you for your time, any coding tip would also be greatly appreaciated. The goal of this program is to show sortign algorithms in motion and comparing them, I wanted to achieve this by having a rather small array to show copied thousands of times (my computer eats a 1k array and spits it out sorted in 0 ms, which isn’t very fun to watch) each one in a thread, this doesn’t however seem to work the way I intended too: slowing down but not lagging;

Anyway sorry for any mistake I may have made throughout this text, English isn’t my first language :slight_smile:

For a starter read my sketch here: Image resize animation and duplication using squareroot size