Commit f698e009 authored by mohammad.salama's avatar mohammad.salama

Finding Primes from 1 to 10^7 using multi-threading

parent c8611a15
......@@ -4,11 +4,11 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="156428c6-b9a7-4d3a-9504-b3537029ac6a" name="Changes" comment="creat">
<change afterPath="$PROJECT_DIR$/src/main/java/SearchForPrimes.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/FindPrimesTest.java" afterDir="false" />
<list default="true" id="156428c6-b9a7-4d3a-9504-b3537029ac6a" name="Changes" comment="Creating Simple Finding Primes in Interval">
<change afterPath="$PROJECT_DIR$/src/main/java/SearchForPrimesThread.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/test/java/SearchForPrimesThreadTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/test/java/FindPrimesTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/test/java/FindPrimesTest.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
......@@ -43,7 +43,7 @@
"last_opened_file_path": "D:/HIAST/FIY/FS/Parallel Programming/Lab/1/PP-lec01-Threads"
}
}]]></component>
<component name="RunManager">
<component name="RunManager" selected="JUnit.SearchForPrimesThreadTest.testSearchForPrimesThreadTest">
<configuration name="FindPrimesTest.testFindPrimes" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab101" />
<option name="PACKAGE_NAME" value="" />
......@@ -54,8 +54,29 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="SearchForPrimesThreadTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab101" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="SearchForPrimesThreadTest" />
<option name="TEST_OBJECT" value="class" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="SearchForPrimesThreadTest.testSearchForPrimesThreadTest" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="Lab101" />
<option name="PACKAGE_NAME" value="" />
<option name="MAIN_CLASS_NAME" value="SearchForPrimesThreadTest" />
<option name="METHOD_NAME" value="testSearchForPrimesThreadTest" />
<option name="TEST_OBJECT" value="method" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="JUnit.SearchForPrimesThreadTest.testSearchForPrimesThreadTest" />
<item itemvalue="JUnit.SearchForPrimesThreadTest" />
<item itemvalue="JUnit.FindPrimesTest.testFindPrimes" />
</list>
</recent_temporary>
......@@ -76,7 +97,14 @@
<option name="project" value="LOCAL" />
<updated>1698433083690</updated>
</task>
<option name="localTasksCounter" value="2" />
<task id="LOCAL-00002" summary="Creating Simple Finding Primes in Interval">
<created>1698436032981</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1698436032981</updated>
</task>
<option name="localTasksCounter" value="3" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">
......@@ -92,6 +120,7 @@
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="first" />
<option name="LAST_COMMIT_MESSAGE" value="first" />
<MESSAGE value="Creating Simple Finding Primes in Interval" />
<option name="LAST_COMMIT_MESSAGE" value="Creating Simple Finding Primes in Interval" />
</component>
</project>
\ No newline at end of file
import java.util.List;
public class SearchForPrimesThread extends Thread
{
private SearchForPrimes searchForPrimes;
private List<Integer> primes;
public SearchForPrimesThread (SearchForPrimes searchForPrimes)
{
this.searchForPrimes = searchForPrimes;
}
@Override
public void run()
{
this.primes = searchForPrimes.getPrimes();
}
public List<Integer> getPrimes()
{
return primes;
}
}
......@@ -12,10 +12,10 @@ public class FindPrimesTest extends TestCase
long endTimer = System.currentTimeMillis();
System.out.println("Time Taken = " + (endTimer-startTimer));
for (int i=0 ; i<Primes1.size() ; i++)
/*for (int i=0 ; i<Primes1.size() ; i++)
{
System.out.println(Primes1.get(i));
}
}*/
}
......
import junit.framework.TestCase;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class SearchForPrimesThreadTest extends TestCase
{
private int Start=1;
private int End=10000000;
private int ThreadsNum=4;
private int step=2500000;
private List<Integer> primes;
public void testSearchForPrimesThreadTest()
{
// ProcessData();
List<SearchForPrimesThread> primesThreads = new ArrayList<>();
for (int i=1 ; i <= ThreadsNum ; i++)
{
int st = (i-1)*step;
int en = i*step;
SearchForPrimes searchForPrimes = new SearchForPrimes(st , en);
SearchForPrimesThread searchForPrimesThread = new SearchForPrimesThread(searchForPrimes);
primesThreads.add(searchForPrimesThread);
}
long startTimer = System.currentTimeMillis();
primes = new ArrayList<>();
for (SearchForPrimesThread searchForPrimesThread : primesThreads)
{
searchForPrimesThread.start();
}
for (SearchForPrimesThread searchForPrimesThread : primesThreads)
{
try
{
searchForPrimesThread.join();
primes.addAll(searchForPrimesThread.getPrimes());
}
catch (InterruptedException e)
{
throw new RuntimeException(e);
}
}
long endTimer = System.currentTimeMillis();
System.out.println("Time Taken = " + (endTimer-startTimer));
/**for (int i=0 ; i<primes.size() ; i++)
{
System.out.println(primes.get(i));
}*/
}
private void ProcessData()
{
System.out.println("Enter Start and End of Interval you want to" +
"look for Prime numbers in! ");
System.out.println("Please Make sure to insert positive integers and start is less than end");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter Start Value : ");
this.Start = scanner.nextInt();
System.out.println("Enter End Value : ");
this.End = scanner.nextInt();
System.out.println("Enter Number of Threads To Parallelize Calculations (Positive Only) ");
this.ThreadsNum = scanner.nextInt();
this.step = (End - Start + 1)/ThreadsNum;
return;
}
}
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