Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
T
Threads_Homework
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
ali.saeed
Threads_Homework
Commits
cbbe988d
Commit
cbbe988d
authored
Oct 29, 2023
by
Ali Saeed
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modify Main.java File
parent
f80017f9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
55 additions
and
2 deletions
+55
-2
Main.java
src/main/java/org/example/Main.java
+55
-2
No files found.
src/main/java/org/example/Main.java
View file @
cbbe988d
package
org
.
example
;
public
class
Main
{
package
org
.
example
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
System
.
out
.
println
(
"Hello world!"
);
System
.
out
.
println
(
"the first case: normal case with 4 threads"
);
FindPrimeNumbersWithMultiThreading
(
1
,
100000000
,
4
);
System
.
out
.
println
(
"the second case: increase the number of threads to 5 without change the search range "
);
FindPrimeNumbersWithMultiThreading
(
1
,
100000000
,
5
);
System
.
out
.
println
(
"the second case: increase the number of threads to 10 without change the search range "
);
FindPrimeNumbersWithMultiThreading
(
1
,
100000000
,
10
);
System
.
out
.
println
(
"the second case: increase the number of threads to 20 without change the search range "
);
FindPrimeNumbersWithMultiThreading
(
1
,
100000000
,
20
);
// System.out.println("the third case: change search range to 50000000 without increase the number of threads");
// FindPrimeNumbersWithMultiThreading(0,50000000,4);
// System.out.println("the third case: change search range to 20000000 without increase the number of threads");
// FindPrimeNumbersWithMultiThreading(0,20000000,4);
// System.out.println("the third case: change search range to 10000000 without increase the number of threads");
// FindPrimeNumbersWithMultiThreading(0,10000000,4);
}
static
public
void
FindPrimeNumbersWithMultiThreading
(
int
a
,
int
b
,
int
threadNumbers
){
List
<
Thread
>
threads
=
new
ArrayList
<>();
int
subRange
=
((
b
-
a
)+
1
)/
threadNumbers
;
ArrayList
<
PrimeNumbersService
>
servers
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
threadNumbers
;
i
++){
int
start
=
i
*
subRange
+
1
;
int
end
=
subRange
*(
i
+
1
);
PrimeNumbersService
x
=
new
PrimeNumbersService
(
start
,
end
);
Thread
thread
=
new
Thread
(
x
);
threads
.
add
(
thread
);
servers
.
add
(
x
);
thread
.
start
();
}
long
startTime
=
System
.
currentTimeMillis
();
for
(
int
i
=
0
;
i
<
threadNumbers
;
i
++){
try
{
threads
.
get
(
i
).
join
();
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
List
<
Integer
>
allPrimeNumbers
=
new
ArrayList
<>();
for
(
PrimeNumbersService
service
:
servers
){
allPrimeNumbers
.
addAll
(
service
.
getPrimeNumbers
());
}
long
endTime
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
"Total prime numbers found: "
+
allPrimeNumbers
.
size
());
System
.
out
.
println
(
"Execution time: "
+
(
endTime
-
startTime
)
+
" milliseconds"
);
}
}
}
}
\ No newline at end of file
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