# How can I use Thread() with functions that has inputs

**URL:** <https://discourse.processing.org/t/how-can-i-use-thread-with-functions-that-has-inputs/12964>\
**Category:** Coding Questions\
**Created:** [July 25, 2019, 8:08am UTC](https://discourse.processing.org/t/how-can-i-use-thread-with-functions-that-has-inputs/12964 "2019-07-25T08:08:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Majorminus](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Majorminus](https://discourse.processing.org/u/Majorminus)\
**Post date:** [July 25, 2019, 8:08am UTC](https://discourse.processing.org/t/how-can-i-use-thread-with-functions-that-has-inputs/12964/1 "2019-07-25T08:08:28Z")

</div>

Hi, I had to run some functions asynchronously and I found a function called “Thread()”.  
I’ve been using it easily with functions that have no inputs like…

```auto
void Something(){
}

thread("Something");

```

But I got a problem when it comes to functions with inputs.

```auto
void SomethingWithInput(int input){
}

```

something like this.

How can I use Thread function correctly in this kind of situation? Or maybe another method that I can run it asynchronously.

Thanks.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 25, 2019, 8:35am UTC](https://discourse.processing.org/t/how-can-i-use-thread-with-functions-that-has-inputs/12964/2 "2019-07-25T08:35:37Z")

</div>

> [@Majorminus](#):
>
> How can I use **thread()** function correctly in this kind of situation?

You can’t, b/c its signature is parameterless & `void`. ⛔

Instead, you can create a `class` which `extends` Thread: 🧵  
[Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.html](http://Docs.Oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Thread.html)

```auto
/**
 * Threaded Method (v1.0.1)
 * GoToLoop (2019/Jan/)
 *
 * https://Discourse.Processing.org/t/
 * how-can-i-use-thread-with-functions-that-has-inputs/12964/2
 */

void setup() {
  noLoop();
}

void draw() {
  background((color) random(#000000));
}

void mousePressed() {
  new ThreadedMethodWithIntParameter((int) random(100));
}

class ThreadedMethodWithIntParameter extends Thread {
  final int input;

  ThreadedMethodWithIntParameter(final int inp) {
    input = inp;
    start();
  }

  @Override void run() {
    println(input);
  }
}

```

---

<div class="post-metadata">

**Author:** ![Majorminus](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Majorminus](https://discourse.processing.org/u/Majorminus)\
**Post date:** [July 26, 2019, 3:13am UTC](https://discourse.processing.org/t/how-can-i-use-thread-with-functions-that-has-inputs/12964/3 "2019-07-26T03:13:54Z")

</div>

I really thank you for your answer. Now I know how to deal with it. But I’m actually using a function with recursive call and I still don’t get it when it comes to this kind of function.

```auto
void QuickSort(int left, int right) {
 if (left <= right) {
 int pivot = Partition(left, right);
 QuickSort(left, pivot - 1);
 QuickSort(pivot + 1, right);
 }
 }
 
 int Partition(int left, int right) {
 int pivot = arr[left];
 int low = left + 1;
 int high = right;
 
 while (low <= high) {
 while (pivot >= arr[low] && low <= right) {
 low++;
 }
 while (pivot <= arr[high] && high >= (left+1)) {
 high--;
 }
 if (low <= high) {
 swap(low, high);
 }
 }
 swap(left, high);
 
 return high;
 }

```

(Quick sort function)

How can I use Thread for a function with recursive calls? Thanks a lot 🙂

---

<div class="post-metadata">

**Author:** ![Majorminus](https://avatars.discourse-cdn.com/v4/letter/m/d6d6ee/32.png) [@Majorminus](https://discourse.processing.org/u/Majorminus)\
**Post date:** [July 26, 2019, 5:26am UTC](https://discourse.processing.org/t/how-can-i-use-thread-with-functions-that-has-inputs/12964/4 "2019-07-26T05:26:24Z")

</div>

I just simply solved it by using another void function

```auto
void QuickSortThread(){
   QuickSort(0,n); 
}

```

I guess it’s not the best way because the inputs cannot be not freely adjusted in this situation

---

<div class="post-metadata">

**Author:** ![KTibow](https://avatars.discourse-cdn.com/v4/letter/k/3e96dc/32.png) [@KTibow](https://discourse.processing.org/u/KTibow)\
**Post date:** [July 27, 2019, 1:35pm UTC](https://discourse.processing.org/t/how-can-i-use-thread-with-functions-that-has-inputs/12964/5 "2019-07-27T13:35:16Z")

</div>

If you still want to have parameters, you can make something kind of like this:

```auto
int thing1;
int thing2;

void setup() {
    thing1 = 5;
    thing2 = 10;
    thread("thing");
}

void loop() {

}

void thing() {
    println(thing1);
    println(thing2);
}

```

You might need a 2D array for recursive sorting.
