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..
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;
}
}
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.
Multithreading in Java is a process of executing two or more threads simultaneously to maximum utilization of CPU. Multithreaded applications execute two or more threads run concurrently. Hence, it is also known as Concurrency in Java.
In a multithreaded process on a single processor, the processor can switch execution resources between threads, resulting in concurrent execution. Concurrency indicates that more than one thread is making progress, but the threads are not actually running simultaneously.
All of your threads can call add at the same time without consequence.
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.
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.
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.
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.
In this case you will need to add special code to make sure your threads behave correctly.
you can think this when different threads call the CollectionTest 's static method 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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With