不同的线程同时调用该方法
2022-09-04 01:49:24
我有下面的java程序来加两个数字。但我试图通过线程进行开发..我一开始就认为应该有五个不同的线程命名为T1,T2,T3,T4,T5,并且所有五个线程应该同时调用add方法,请建议我如何实现这一点,所有五个线程应该同时调用add方法,以便提高性能。
有人可以建议我如何通过执行器框架或contdown闩锁来实现这一点吗?
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;
}
}