Java 不同线程同时调用该方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19674093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 19:18:02  来源:igfitidea点击:

Different threads calling the method at the same time

javamultithreadingperformance

提问by sonum kumar

I have the below java program to add two numbers..but I was trying to develop through threads that is ..I was looking that at the start there should be five different threads named T1,T2,T3,T4,T5 and all the five threads should call the add method at the same time, please advise how I can achieve this that all the five threads should call the add method at the same time, so that performance could be improve..

我有下面的 java 程序来添加两个数字。五个线程应该同时调用 add 方法,请告知我如何实现这一点,所有五个线程应该同时调用 add 方法,以便提高性能..

can somebody please advise how can I achieve this through the executor framework or contdown latch

有人可以建议我如何通过执行器框架或控制闩锁来实现这一点

public class CollectionTest {

    public static void main(String args[]) {

        //create Scanner instance to get input from User
        Scanner scanner = new Scanner(System.in);

        System.err.println("Please enter first number to add : ");
        int number = scanner.nextInt();

        System.out.println("Enter second number to add :");
        int num = scanner.nextInt();

        //adding two numbers in Java by calling method
       int result = add(number, num);

       System.out.printf(" Addition of numbers %d and %d is %d %n", number, num, result);
    }

    public static int add(int number, int num){
        return number + num;
    }
} 

回答by Numeron

All of your threads can call add at the same time without consequence.

您的所有线程都可以同时调用 add ,而不会产生任何后果。

This is because inside the method the number and num variables are local to that method only - and also to the caller thread. If number and/or num were global it would be a different story.

这是因为在方法内部, number 和 num 变量仅对该方法是本地的 - 并且对于调用者线程也是如此。如果 number 和/或 num 是全局的,那将是另一回事。

edit:

编辑:

for example, in this case:

例如,在这种情况下:

public static int add(int number, int num){
    //A
    return number + num;
}

When a thread gets to point A, it has its two numbers that were passed in.

当一个线程到达点 A 时,它有两个传入的数字。

When a different thread gets to point A, it has called its own version of the method with its own different numbers that were passed in. This means they have no effect of what the first thread is doing.

当不同的线程到达点 A 时,它会调用自己版本的方法,并使用传入的不同数字。这意味着它们对第一个线程正在执行的操作没有影响。

The numbers are local to the method only.

这些数字仅适用于该方法。

in this case:

在这种情况下:

static int number;

public static int add(int num){
    //A
    return number + num;
}

When a thread gets to point A, it has the num passed in, and it has the outside number because the outside number is accessable to all. If another thread enters the method, it will have its own num (because it called its own version of the method) but it will be using the same outside number because that number is global and accessable to all.

当一个线程到达点 A 时,它传入了 num,并且它具有外部编号,因为外部编号可供所有人访问。如果另一个线程进入该方法,它将有自己的 num(因为它调用了自己的方法版本),但它将使用相同的外部编号,因为该编号是全局的并且可供所有人访问。

In this case you will need to add special code to make sure your threads behave correctly.

在这种情况下,您需要添加特殊代码以确保您的线程正确运行。

回答by user2484362

you can think this when different threads call the CollectionTest 's static method add

当不同的线程调用 CollectionTest 的静态方法 add 时,你可以这样想

then what will occurs: for exmaple:

那么会发生什么:例如:

public class Test {
/**
 * @param args
 */
public static void main(String[] args) {
    Runnable t1 = new Runnable() {
        public void run() {
            CollectionTest.add(1, 2);
        }
    };

    Runnable t2 = new Runnable() {
        public void run() {
            CollectionTest.add(3, 4);

        }
    };


    new Thread(t1).start();
    new Thread(t2).start();

}
}


public static int add(int number, int num){
    // when different thread call method 
    // for example   
    //  Runnable t1 call ,then "number" will be assigned 1, "num" will be assigned 2
    //  number ,num will keep in thread'stack spack
    return number + num;
}