Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
L
Lab1
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mohammad.salama
Lab1
Commits
f698e009
Commit
f698e009
authored
Oct 27, 2023
by
mohammad.salama
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Finding Primes from 1 to 10^7 using multi-threading
parent
c8611a15
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
137 additions
and
9 deletions
+137
-9
workspace.xml
.idea/workspace.xml
+36
-7
SearchForPrimesThread.java
src/main/java/SearchForPrimesThread.java
+21
-0
FindPrimesTest.java
src/test/java/FindPrimesTest.java
+2
-2
SearchForPrimesThreadTest.java
src/test/java/SearchForPrimesThreadTest.java
+78
-0
No files found.
.idea/workspace.xml
View file @
f698e009
...
...
@@ -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/
FindPrimes
Test.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/SearchForPrimes
Thread
.java"
afterDir=
"false"
/>
<change
afterPath=
"$PROJECT_DIR$/src/test/java/
SearchForPrimesThread
Test.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
src/main/java/SearchForPrimesThread.java
0 → 100644
View file @
f698e009
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
;
}
}
src/test/java/FindPrimesTest.java
View file @
f698e009
...
...
@@ -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));
}
}
*/
}
...
...
src/test/java/SearchForPrimesThreadTest.java
0 → 100644
View file @
f698e009
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
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment