Commit 42141088 authored by amira.alkatably's avatar amira.alkatably

First Commit

parents
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_19" default="true" project-jdk-name="19" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>PP-01-Threads</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>19</maven.compiler.source>
<maven.compiler.target>19</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>
\ No newline at end of file
public class FibonacciCalculator implements Runnable {
private int n;
private long result;
public FibonacciCalculator(int n) {
this.n = n;
}
public long getResult() {
return result;
}
@Override
public void run() {
result = fibonacci(n);
}
public long fibonacci(int n) {
if (n <= 1) {
return n;
} else {
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
}
public class PrimeGenerator implements Runnable {
private int start;
private int end;
private boolean[] primes;
public PrimeGenerator(int start, int end, boolean[] primes) {
this.start = start;
this.end = end;
this.primes = primes;
}
@Override
public void run() {
for (int i = start; i <= end; i++) {
primes[i] = isPrime(i);
}
}
private boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
import junit.framework.TestCase;
public class FibonacciCalculatorTest extends TestCase {
public void testFibonacciCalculator(){
int n = 50; // Change this to calculate different Fibonacci numbers
FibonacciCalculator fib = new FibonacciCalculator(n);
Thread thread1 = new Thread(fib);
long startTime = System.currentTimeMillis();
thread1.start();
try {
thread1.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
long endTime = System.currentTimeMillis();
long result = fib.getResult();
System.out.println("Fibonacci number at position " + n + " is: " + result);
System.out.println("\nTime taken: " + (endTime - startTime) + " milliseconds");
}
public void testFibonacciCalculatorUsingThreads(){
int n = 50; // Change this to calculate different Fibonacci numbers
FibonacciCalculator fib1 = new FibonacciCalculator(n - 1);
FibonacciCalculator fib2 = new FibonacciCalculator(n - 2);
Thread thread1 = new Thread(fib1);
Thread thread2 = new Thread(fib2);
long startTime = System.currentTimeMillis();
thread1.start();
thread2.start();
try {
thread1.join();
thread2.join();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
long result = fib1.getResult() + fib2.getResult();
long endTime = System.currentTimeMillis();
System.out.println("Fibonacci number at position " + n + " is: " + result);
System.out.println("\nTime taken: " + (endTime - startTime) + " milliseconds");
}
}
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.List;
public class PrimeGeneratorTest extends TestCase {
public void testPrimeGeneratorUsing4Threads(){
int n = 100000000; // Range is from 1 to 100,000,000
int numThreads = 4; // Number of Threads
boolean[] primes = new boolean[n + 1];
Thread[] threads = new Thread[numThreads];
int blockSize = n / numThreads;
long startTime = System.currentTimeMillis();
for (int i = 0; i < numThreads; i++) {
int start = i * blockSize + 1;
int end = (i == numThreads - 1) ? n : (i + 1) * blockSize;
threads[i] = new Thread(new PrimeGenerator(start, end, primes));
threads[i].start();
}
try {
for (Thread thread : threads) {
thread.join();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// The fifth thread collects the results
List<Integer> finalPrimes = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
if(primes[i]) finalPrimes.add(i);
}
long endTime = System.currentTimeMillis();
// for (int i : finalPrimes) {
// System.out.print(i + " ");
// }
System.out.println("100,000,000 With 4 Threads: Time taken is " + (endTime - startTime) + " milliseconds");
}
public void testPrimeGeneratorUsing2Threads(){
int n = 100000000; // Range is from 1 to 100,000,000
int numThreads = 2; // Number of Threads
boolean[] primes = new boolean[n + 1];
Thread[] threads = new Thread[numThreads];
int blockSize = n / numThreads;
long startTime = System.currentTimeMillis();
for (int i = 0; i < numThreads; i++) {
int start = i * blockSize + 1;
int end = (i == numThreads - 1) ? n : (i + 1) * blockSize;
threads[i] = new Thread(new PrimeGenerator(start, end, primes));
threads[i].start();
}
try {
for (Thread thread : threads) {
thread.join();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// The fifth thread collects the results
List<Integer> finalPrimes = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
if(primes[i]) finalPrimes.add(i);
}
long endTime = System.currentTimeMillis();
System.out.println("100,000,000 With 2 Threads: Time taken is " + (endTime - startTime) + " milliseconds");
}
public void testPrimeGeneratorUsing5Threads(){
int n = 100000000; // Range is from 1 to 100,000,000
int numThreads = 5; // Number of Threads
boolean[] primes = new boolean[n + 1];
Thread[] threads = new Thread[numThreads];
int blockSize = n / numThreads;
long startTime = System.currentTimeMillis();
for (int i = 0; i < numThreads; i++) {
int start = i * blockSize + 1;
int end = (i == numThreads - 1) ? n : (i + 1) * blockSize;
threads[i] = new Thread(new PrimeGenerator(start, end, primes));
threads[i].start();
}
try {
for (Thread thread : threads) {
thread.join();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// The fifth thread collects the results
List<Integer> finalPrimes = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
if(primes[i]) finalPrimes.add(i);
}
long endTime = System.currentTimeMillis();
System.out.println("100,000,000 With 5 Threads: Time taken is " + (endTime - startTime) + " milliseconds");
}
public void testPrimeGeneratorUsing8Threads(){
int n = 100000000; // Range is from 1 to 100,000,000
int numThreads = 8; // Number of Threads
boolean[] primes = new boolean[n + 1];
Thread[] threads = new Thread[numThreads];
int blockSize = n / numThreads;
long startTime = System.currentTimeMillis();
for (int i = 0; i < numThreads; i++) {
int start = i * blockSize + 1;
int end = (i == numThreads - 1) ? n : (i + 1) * blockSize;
threads[i] = new Thread(new PrimeGenerator(start, end, primes));
threads[i].start();
}
try {
for (Thread thread : threads) {
thread.join();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// The fifth thread collects the results
List<Integer> finalPrimes = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
if(primes[i]) finalPrimes.add(i);
}
long endTime = System.currentTimeMillis();
System.out.println("100,000,000 With 8 Threads: Time taken is " + (endTime - startTime) + " milliseconds");
}
public void testPrimeGeneratorUsing4ThreadsWithSmallerRange(){
int n = 50000000; // Range is from 1 to 100,000,000
int numThreads = 4; // Number of Threads
boolean[] primes = new boolean[n + 1];
Thread[] threads = new Thread[numThreads];
int blockSize = n / numThreads;
long startTime = System.currentTimeMillis();
for (int i = 0; i < numThreads; i++) {
int start = i * blockSize + 1;
int end = (i == numThreads - 1) ? n : (i + 1) * blockSize;
threads[i] = new Thread(new PrimeGenerator(start, end, primes));
threads[i].start();
}
try {
for (Thread thread : threads) {
thread.join();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// The fifth thread collects the results
List<Integer> finalPrimes = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
if(primes[i]) finalPrimes.add(i);
}
long endTime = System.currentTimeMillis();
System.out.println("50,000,000 With 4 Threads: Time taken is " + (endTime - startTime) + " milliseconds");
}
public void testPrimeGeneratorUsing4ThreadsWithBiggerRange(){
int n = 200000000; // Range is from 1 to 100,000,000
int numThreads = 4; // Number of Threads
boolean[] primes = new boolean[n + 1];
Thread[] threads = new Thread[numThreads];
int blockSize = n / numThreads;
long startTime = System.currentTimeMillis();
for (int i = 0; i < numThreads; i++) {
int start = i * blockSize + 1;
int end = (i == numThreads - 1) ? n : (i + 1) * blockSize;
threads[i] = new Thread(new PrimeGenerator(start, end, primes));
threads[i].start();
}
try {
for (Thread thread : threads) {
thread.join();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// The fifth thread collects the results
List<Integer> finalPrimes = new ArrayList<Integer>();
for (int i = 1; i <= n; i++) {
if(primes[i]) finalPrimes.add(i);
}
long endTime = System.currentTimeMillis();
System.out.println("200,000,000 With 4 Threads: Time taken is " + (endTime - startTime) + " milliseconds");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment