0% found this document useful (0 votes)
9 views5 pages

9 (A)

The document outlines a multi-threaded Java program that calculates the Fibonacci series up to the n-th term by utilizing multiple threads for efficient computation. It details the algorithm, including reading user input, initializing the Fibonacci array, and dividing the calculation across threads based on available CPU cores. The program employs an iterative approach to avoid recursion issues and ensures all threads complete before printing the results.

Uploaded by

sruthirajesh456
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

9 (A)

The document outlines a multi-threaded Java program that calculates the Fibonacci series up to the n-th term by utilizing multiple threads for efficient computation. It details the algorithm, including reading user input, initializing the Fibonacci array, and dividing the calculation across threads based on available CPU cores. The program employs an iterative approach to avoid recursion issues and ensures all threads complete before printing the results.

Uploaded by

sruthirajesh456
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EXPNO: 9(a) MULTITHREADING

DATE:

AIM:
To implement a *multi-threaded function* that calculates the *Fibonacci series* up to the *n-
th term* using *multiple threads*, where each thread computes a portion of the series
iteratively and merges the results.

BACKGROUND THEORY:
Multi-threading is a technique that allows multiple computations to be performed
concurrently, utilizing CPU cores efficiently. Fibonacci sequence follows the recurrence
relation:
F(n) = F(n-1) + F(n-2)

with base values F(0) = 0 and F(1) = 1.


Thread optimization: The number of threads is selected based on *available CPU cores* to
enhance performance without causing excessive context switching.
Iterative approach is used instead of recursion to avoid excessive function calls and stack
overflow errors for large n..

ALGORITHM:
1. Read an integer n from the user, where 1 ≤ n ≤ 10⁶.

2. Initialize an array to store the Fibonacci sequence.

3. Set base cases:

- fib[0] = 0

- fib[1] = 1 (if n > 1)

4. Determine the number of threads based on the available CPU cores.

5. Divide the Fibonacci calculation across threads by assigning chunks of indices to different
threads.

6. Start each thread, where each computes Fibonacci numbers for its assigned range.

7. Wait for all threads to finish execution using join().


8. Print the computed Fibonacci sequence in order.
CODING:
import java.util.Arrays;

import java.util.Scanner;

class FibonacciThread extends Thread {

private final long[] fibArray;

private final int start, end;


public FibonacciThread(long[] fibArray, int start, int end) {

this.fibArray = fibArray;

this.start = start;

this.end = end;

public void run() {

for (int i = start; i < end; i++) {


fibArray[i] = fibArray[i - 1] + fibArray[i - 2];

public class MultiThreadedFibonacci {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);


int n = scanner.nextInt();

scanner.close();

if (n < 1) {

System.out.println("Invalid input");

return;

long[] fibArray = new long[n];

fibArray[0] = 0;
if (n > 1) {
fibArray[1] = 1;

int cores = Runtime.getRuntime().availableProcessors();

int numThreads = Math.min(cores, n / 2); // Optimize thread count


FibonacciThread[] threads = new FibonacciThread[numThreads];

int chunkSize = n / numThreads;

int start = 2;

for (int i = 0; i < numThreads; i++) {

int end = (i == numThreads - 1) ? n : start + chunkSize;

threads[i] = new FibonacciThread(fibArray, start, end);

threads[i].start();
start = end;
}

for (FibonacciThread thread : threads) {

try {

thread.join();

} catch (InterruptedException e) {

e.printStackTrace();

}
}]

for (long num : fibArray) {

System.out.println(num);

}
OUTPUT:
RESULT:
Thus, the multi-threaded Java program efficiently finds all prime numbers in the given range
by distributing the task across multiple threads.

You might also like