java exp no 9
java exp no 9
9(a)
DATE –
Aim:
In this task, you are required to implement a multi-threaded function that
calculates the Fibonacci series up to the nth term. The calculation should be
distributed across multiple threads, with each thread calculating a portion of the
Fibonacci series. Each thread will compute specific terms, and the results should
be merged to form the complete series.
Background Theory:
The Fibonacci sequence is a series of numbers where each number is the sum of
the two preceding ones.
12.Iterate through the fib array and print each Fibonacci number.
Coding:
import java.util.*;
import java.util.concurrent.*;
this.start = start;
this.end = end;
this.fib = fib; }
synchronized (fib) {
int n = sc.nextInt();
if (n < 1) {
System.out.println("Invalid input");
return; }
fib[0] = 0;
if (n > 1) fib[1] = 1;
threads.add(t);
t.start();
}
for (Thread t : threads) {
t.join();
}
executor.shutdown();
System.out.println(num);
}
}
}
Output:
Result:
DATE –
Aim:
Background Theory:
1. The sum of the first N Fibonacci numbers (using the fibo thread).
2. The sum of the first N prime numbers (using the prime thread).
Concepts Used:
Threads in Java: The program creates two separate threads (fibo and
prime) to perform calculations concurrently.
Fibonacci Sequence: Each number is the sum of the two preceding
numbers. The sum is calculated iteratively.
Prime Numbers: A number is prime if it has only two divisors: 1 and
itself. The program finds and sums the first N prime numbers.
Concurrency: Both calculations run in parallel, improving efficiency.
Algorithm:
Step 1: Take Input
2. Create a fibo thread to compute the sum of the first N Fibonacci numbers.
3. Create a prime thread to compute the sum of the first N prime numbers.
4. Start both threads using start(), allowing them to run concurrently.
package workout;
import java.util.*;
fibo(int n){
this.n=n;
this.first=0;
this.second=1;
this.res=1;
}
public void run() {
try {
Thread.sleep(1000);
for(int i=2;i<n;i++) {
int temp=first+second;
first=second;
second=temp;
res+=temp;
}
System.out.println("The sum of fibonacci series is "+ res); }
catch(Exception e) { } } }
prime(int n) {
this.n = n;
res = 0;
count = 0;
num = 2; // Start checking from 2
primesFound = 0;
}
}
Output:
Result:
Thus, the program correctly computes the sum of the first N prime numbers
and the sum of the first N Fibonacci numbers concurrently,
EXP NO – Implementation of multi threading
9(c)
DATE –
Aim:
To implement a multi-threaded program in C++ that efficiently finds and
displays all prime numbers within a given range [n, m] by dividing the workload
between multiple threads, optimizing execution time.
Background Theory:
Concepts Used:
Algorithm:
#include <iostream>
#include <thread>
int main() {
cin >> n >> m;
if (n > m) {
cout << "Invalid range" << endl;
return 0;
}
t1.join();
t2.join();
return 0;
}
Output:
Result:
Thus, the program correctly finds and displays all prime numbers within the
given range using optimized multi-threading.