Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you call the same method on different threads?

Really noob question I know but I'm trying to understand how methods and threads work as an amateur programmer. I'm sure this is a fundamental lack of understanding on my part but hopefully some nice person can put me straight.

What I was wondering is if you call the same method multiple times using multiple threads do you create sandboxed versions of each method which act independently of each other or can they interfere with each other. For example I've knocked up some very simple code below to try to illustrate what I mean.

So in the example we have a method that is called when a button is clicked. It takes two numbers and feeds them into a second method that adds them together and returns the result. This seems straight forward. But imagine we wanted to do another calculation using the same method but we didn't want to wait for the first calculation to finish. We could invoke the method that adds the numbers on a separate thread so it doesn't hold up the UI thread. Cool. Ok but what if we do this twice? or three times?

What I'm trying to ask is when "doSum" is called the first time below the numbers passed into it are 10 and 20. The code runs the method on a separate thread and should return the answer 30. The second time it is called the numbers are 30 and 50 and the result should be 80. If for some reason the calculation in the first thread was still going on does it get overwritten when I call the same method a second time? Would result1 ever be in danger of being returned as 80 or 140?

Does this make sense to anyone?

public void onbuttonclicked(View v) {

int number1;
int number2;
int result1, result2, result3;


//first callculation --------------------------------------
number1 = 10;
number2 = 20;
    Thread t1 = new Thread(new Runnable() {
        public void run() {
        result1 = doSum(number1, number2);
            }
    });
    t1.start();


//second callculation -----------------------------------
number1 = 30;
number2 = 50;
    Thread t2 = new Thread(new Runnable() {
        public void run() {
        result2 = doSum(number1, number2);
            }
    });
    t2.start();

//third callculation -----------------------------------------
number1 = 60;
number2 = 80;
    Thread t3 = new Thread(new Runnable() {
        public void run() {
        result3 = doSum(number1, number2);
            }
    });
    t3.start();


}


public static int doSum(int a, int b)
{
    int result = a + b;
    return result;
}
like image 212
Regnodulous Avatar asked Jan 10 '23 14:01

Regnodulous


1 Answers

There are primarily 2 things you should know.

  1. If 2 threads call the same method, each thread will have a different Stack Frame for the method. So, method local variables are thread safe. The changes made in local variables of one method will not interfere with other thread's changes.

  2. You should (usually) worry about thread-safety / interference when you have a shared resource being modified by both threads.

PS : Your doSum() does very little processing. A smart JVM might actually inline the method.

like image 99
TheLostMind Avatar answered Jan 18 '23 02:01

TheLostMind