Passing a parameter through a thread

Hi again everyone, currently stuck on a problem, I wish to create multiple threads to maximize cpu usage as I understand processing only uses the one thread by default, (perhaps this is java by default). I have done some research and converted a piece of code I found to work in processing.

class A implements Runnable {
  String to_print;
  int t;
  A(String to_print) {
    this.to_print = to_print;
  }
  
  A(Integer t) {
    this.t = t;
  }
  //public void run() {
  //  System.out.println(to_print);
  //}
}

String [] messages = {"Test string for secondary thread",
                      "Here's another message.",
                      "And this is yet another thread."};

int [] numbers = { 0,1,2,3,4,5,6,7,8,9,10};

void setup(){
  size(200,200);
  //new Thread(new A("Test string for secondary thread")).start();
  //new Thread(new A("Here's another message.")).start();
  //new Thread(new A("And this is yet another thread.")).start();
  //for(int i=0;i<messages.length;i++){
  //  String message = messages[i];
    
  //  new Thread(new A(message)).start();
  //}
  for(int i=0;i<numbers.length;i++){
    int number = numbers[i];
    
    new Thread(new A(number)).start();
  }
};

void draw(){
  
};

//  public static void main(String[] args) {
//    System.out.println("In primary Thread");
//    new Thread(new A("Test string for secondary thread")).start();
//    new Thread(new A("Here's another message.")).start();
//    new Thread(new A("And this is yet another thread.")).start();
//};

Now this works for strings, but does not work for integers and therefore I’m assuming perhaps wrongly that it doesn’t work for other object types.

Please try to keep the explanation simple, i really struggle wrapping my head around the structure of the java language at the moment.

1 Like

Yikes!!! My run function is calling the wrong variable…

2 Likes