Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
PP-01-Threads-Review
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
mohamadbashar.disoki
PP-01-Threads-Review
Commits
fbe1a1b6
You need to sign in or sign up before continuing.
Commit
fbe1a1b6
authored
Nov 01, 2023
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Prime Numbers Finder Example
parent
661fb28f
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
137 additions
and
1 deletion
+137
-1
PrimeFinder.java
src/main/java/PrimeFinder.java
+40
-0
PrimeNumTest.java
src/test/java/PrimeNumTest.java
+97
-0
StudentServiceTest.java
src/test/java/StudentServiceTest.java
+0
-1
No files found.
src/main/java/PrimeFinder.java
0 → 100644
View file @
fbe1a1b6
import
java.util.ArrayList
;
import
java.util.List
;
public
class
PrimeFinder
{
private
int
start
;
private
int
end
;
private
List
<
Integer
>
primes
;
public
PrimeFinder
(
int
start
,
int
end
)
{
this
.
start
=
start
;
this
.
end
=
end
;
this
.
primes
=
new
ArrayList
<>();
}
public
List
<
Integer
>
getPrimes
()
{
for
(
int
i
=
this
.
start
;
i
<=
this
.
end
;
i
++)
{
boolean
isPrime
=
true
;
for
(
int
j
=
2
;
j
<=
Math
.
sqrt
(
i
);
j
++)
{
if
(
i
%
j
==
0
)
{
isPrime
=
false
;
break
;
}
}
if
(
isPrime
)
{
primes
.
add
(
i
);
}
}
return
this
.
primes
;
}
}
src/test/java/PrimeNumTest.java
0 → 100644
View file @
fbe1a1b6
import
junit.framework.TestCase
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
PrimeNumTest
extends
TestCase
{
public
void
testFindPrimesSeq
()
{
PrimeFinder
primeFinder
=
new
PrimeFinder
(
1
,
100_000_000
);
long
startTime
=
System
.
currentTimeMillis
();
List
<
Integer
>
Primes1
=
primeFinder
.
getPrimes
();
long
end
=
System
.
currentTimeMillis
()
-
startTime
;
System
.
out
.
println
(
"Time Taken = "
+
end
);
System
.
out
.
println
(
Primes1
.
size
());
assertEquals
(
5761456
,
Primes1
.
size
());
}
public
void
testChunksRange
(){
int
[]
rangeLength
=
{
100_000_000
,
10_000_000
,
1_000_000
};
int
[]
NumThreads
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
20
,
30
,
40
};
for
(
int
rl:
rangeLength
){
for
(
int
nt
:
NumThreads
){
System
.
out
.
printf
(
"Range Length: %-9d, NumThread: %d \n"
,
rl
,
nt
);
List
<
Thread
>
primeTh
=
new
ArrayList
<>();
for
(
int
n
=
0
;
n
<
nt
;
++
n
)
{
int
start
=
getChunkStart
(
n
,
nt
,
rl
);
int
end
=
getChunkEnd
(
n
,
nt
,
rl
);
System
.
out
.
println
(
start
+
" "
+
end
);
}
}
}
}
public
void
testFindPrimesThread
(){
int
[]
rangeLength
=
{
100_000_000
,
10_000_000
,
1_000_000
};
int
[]
NumThreads
=
{
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
20
,
40
,
100
,
200
,
500
,
1000
};
for
(
int
rl:
rangeLength
){
for
(
int
nt
:
NumThreads
){
System
.
out
.
printf
(
"Range Length: %-9d, NumThread: %d \n"
,
rl
,
nt
);
List
<
Thread
>
primeTh
=
new
ArrayList
<>();
for
(
int
n
=
0
;
n
<
nt
;
++
n
)
{
int
start
=
getChunkStart
(
n
,
nt
,
rl
);
int
end
=
getChunkEnd
(
n
,
nt
,
rl
);
PrimeFinder
primeFinder
=
new
PrimeFinder
(
start
,
end
);
Thread
th
=
new
Thread
(()
->
primeFinder
.
getPrimes
());
primeTh
.
add
(
th
);
}
long
StartTime
=
System
.
currentTimeMillis
();
for
(
Thread
primeFinderTh
:
primeTh
){
primeFinderTh
.
start
();
}
for
(
Thread
primeFinderTh
:
primeTh
){
try
{
primeFinderTh
.
join
();
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
long
end
=
System
.
currentTimeMillis
()-
StartTime
;
System
.
out
.
println
(
"Time Taken: "
+
end
);
}
}
}
private
static
int
getChunkSize
(
final
int
nChunks
,
final
int
nElements
)
{
return
(
nElements
+
nChunks
-
1
)
/
nChunks
;
}
private
static
int
getChunkStart
(
final
int
chunk
,
final
int
nChunks
,
final
int
nElements
)
{
final
int
chunkSize
=
getChunkSize
(
nChunks
,
nElements
);
return
(
chunk
*
chunkSize
)+
1
;
}
private
static
int
getChunkEnd
(
final
int
chunk
,
final
int
nChunks
,
final
int
nElements
)
{
final
int
chunkSize
=
getChunkSize
(
nChunks
,
nElements
);
final
int
end
=
(
chunk
+
1
)
*
chunkSize
;
if
(
end
>
nElements
)
{
return
nElements
;
}
else
{
return
end
;
}
}
}
src/test/java/StudentServiceTest.java
View file @
fbe1a1b6
import
junit.framework.TestCase
;
import
junit.framework.TestCase
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
...
...
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