Addition Program Call by Reference Using Multithreading in Java
First, let's define what call by reference means. In Java, all objects are passed by reference, which means that when a method is called and an object is passed as an argument, the method receives a reference to the memory location of the object, rather than a copy of the object itself. This allows the method to make changes to the original object, rather than just working with a copy of it. Pass by value refers to the process of passing a variable's value alone as an argument to a function. In this scenario, any modification to a parameter's value in the called method has no impact on the value in the calling method.
Now, let's talk about how we can use call by reference in a multithreaded program. A multithreaded program is one that uses multiple threads of execution to perform different tasks simultaneously. In Java, this can be achieved by implementing the Runnable interface and creating multiple Thread objects.
Here is an example of a simple program that uses call by reference in a multithreaded context
Filename: Counter1.java
class Counter {
int count = 0;
public void increment() {
count++;
}
}
class Incrementer implements Runnable {
Counter counter;
public Incrementer(Counter c) {
counter = c;
}
public void run() {
for (int i = 0; i < 10000; i++) {
counter.increment();
}
}
}
public class Counter1 {
public static void main(String[] args) {
Counter c = new Counter();
Thread t1 = new Thread(new Incrementer(c));
Thread t2 = new Thread(new Incrementer(c));
t1.start();
t2.start();
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(c.count);
}
}
Output:
17005
In this example, we have a Counter class with a single integer field count and a method increment() that increases the value of count by 1. We also have an Incrementer class that implements the Runnable interface and takes a Counter object as an argument in its constructor. The run() method of the Incrementer class increments the count field of the Counter object 10000 times.
In the main() method, we create a Counter object and two Thread objects, passing an Incrementer object that takes the Counter object as an argument to the constructors of the Thread objects. We then start both threads and use the join() method to wait for both threads to complete. Finally, we print the value of the count field of the Counter object, which should be 20000 if the program works correctly. In this example, we passed the counter object to the Incrementer class which is then passed to the Thread constructors. Therefore, both threads are modifying the same counter object, and the final value of count should be 20000.
Here is an example of an addition program that uses call by reference and multithreading in Java:
Filename: Counter2.java
class Addition {
int sum;
public void add(int a, int b) {
sum = a + b;
}
}
class MultiThreading implements Runnable {
Thread t;
Addition add;
MultiThreading(Addition add) {
this.add = add;
t = new Thread(this);
t.start();
}
public void run() {
add.add(5, 10);
}
}
public class Counter2 {
public static void main(String[] args) {
Addition add = new Addition();
MultiThreading mt = new MultiThreading(add);
try {
mt.t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Sum: " + add.sum);
}
}
Output:
Sum: 15
In this example, the Addition class has a single method add which takes two integers as input and adds them to the instance variable sum.
The MultiThreading class implements the Runnable interface, which allows it to be executed by a thread. It takes an instance of the Addition class as input and starts a new thread with this instance. In the run method, it calls the add method on the instance of the Addition class passed to it. In the main method, an instance of the Addition class is created, and an instance of the MultiThreading class is created with this instance as input.
The join method is called on the thread of the MultiThreading instance, which causes the main thread to wait for the thread in MultiThreading to finish execution before proceeding. Finally, the value of the sum is printed.
In conclusion, using call by reference in a multithreaded program allows multiple threads to access and modify the same objects simultaneously, which can be useful for achieving certain types of parallel processing.