Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
E
ExecutorServiceHW
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
laith.asaad
ExecutorServiceHW
Commits
d57be0bb
Commit
d57be0bb
authored
Nov 15, 2023
by
Lenovo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
third step
parent
8c848e4a
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
258 additions
and
0 deletions
+258
-0
ParallelSummer0.java
...ain/java/org/example/parallelsummers/ParallelSummer0.java
+53
-0
ParallelSummer1.java
...ain/java/org/example/parallelsummers/ParallelSummer1.java
+48
-0
ParallelSummer2.java
...ain/java/org/example/parallelsummers/ParallelSummer2.java
+46
-0
ParallelSummer3.java
...ain/java/org/example/parallelsummers/ParallelSummer3.java
+59
-0
ParallelSummer4.java
...ain/java/org/example/parallelsummers/ParallelSummer4.java
+52
-0
No files found.
src/main/java/org/example/parallelsummers/ParallelSummer0.java
0 → 100644
View file @
d57be0bb
package
org
.
example
.
parallelsummers
;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
org.example.summers.SummerRunnable
;
import
org.example.summers.SummerThread
;
import
org.example.worker.WorkPartitioner
;
public
class
ParallelSummer0
{
/*
The sum function is used to distribute the task among threads for the completion of the mission.
The result of this operation is the count of prime numbers.
*/
public
static
long
sum
(
int
[]
data
,
int
threadCount
)
{
List
<
SummerThread
>
summers
=
new
ArrayList
<>();
List
<
WorkPartitioner
.
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
for
(
WorkPartitioner
.
Part
part
:
parts
)
{
summers
.
add
(
new
SummerThread
(
data
,
part
));
}
for
(
SummerThread
summerThread
:
summers
)
{
summerThread
.
start
();
}
for
(
SummerThread
summerThread
:
summers
)
{
try
{
summerThread
.
join
();
}
catch
(
InterruptedException
e
)
{
System
.
err
.
println
(
"Thread cannot join!"
);
}
}
long
sum
=
0
;
for
(
SummerThread
summerThread
:
summers
)
{
sum
+=
summerThread
.
getSum
();
}
System
.
out
.
println
(
"summer0 "
+
sum
);
return
sum
;
}
}
src/main/java/org/example/parallelsummers/ParallelSummer1.java
0 → 100644
View file @
d57be0bb
package
org
.
example
.
parallelsummers
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.example.summers.SummerRunnable
;
import
org.example.summers.SummerThread
;
import
org.example.worker.WorkPartitioner
;
public
class
ParallelSummer1
{
public
static
long
sum
(
int
[]
data
,
int
threadCount
)
{
/*
The sum function is used to distribute the task among threads for the completion of the mission.
The result of this operation is the count of prime numbers.
Note that the function use SummerRunnable which it's run methods return void,
so you have to use for loop to get the result.
*/
List
<
SummerRunnable
>
summers
=
new
ArrayList
<>();
List
<
WorkPartitioner
.
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
for
(
WorkPartitioner
.
Part
part
:
parts
)
{
summers
.
add
(
new
SummerRunnable
(
data
,
part
));
}
for
(
SummerRunnable
SummerRunnable
:
summers
)
{
SummerRunnable
.
startThread
();
}
for
(
SummerRunnable
SummerRunnable
:
summers
)
{
try
{
SummerRunnable
.
joinThread
();
}
catch
(
InterruptedException
e
)
{
System
.
err
.
println
(
"Thread cannot join!"
);
}
}
long
sum
=
0
;
for
(
SummerRunnable
summerRunnable
:
summers
)
{
sum
+=
summerRunnable
.
getSum
();
}
System
.
out
.
println
(
"summer1 "
+
sum
);
return
sum
;
}
}
src/main/java/org/example/parallelsummers/ParallelSummer2.java
0 → 100644
View file @
d57be0bb
package
org
.
example
.
parallelsummers
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.stream.Collectors
;
import
org.example.summers.SummerThread
;
import
org.example.worker.WorkPartitioner
;
public
class
ParallelSummer2
{
public
static
long
sum
(
int
[]
data
,
int
threadCount
)
{
/*
The sum function is used to distribute the task among threads for the completion of the mission.
The result of this operation is the count of prime numbers.
Note that the function use SummerThread which it's run methods return void,
so you have to use for loop to get the result.
*/
List
<
SummerThread
>
summers
=
new
ArrayList
<>();
List
<
WorkPartitioner
.
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
for
(
WorkPartitioner
.
Part
part
:
parts
)
{
summers
.
add
(
new
SummerThread
(
data
,
part
));
}
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
threadCount
);
for
(
SummerThread
summer:
summers
)
{
executor
.
execute
(
summer
);
}
executor
.
shutdown
();
//Waiting for all the thread to finalize
while
(!
executor
.
isTerminated
())
{
}
System
.
out
.
println
(
"summer2 "
+
summers
.
stream
().
mapToLong
(
SummerThread:
:
getSum
).
sum
());
return
summers
.
stream
().
mapToLong
(
SummerThread:
:
getSum
).
sum
();
}
}
src/main/java/org/example/parallelsummers/ParallelSummer3.java
0 → 100644
View file @
d57be0bb
package
org
.
example
.
parallelsummers
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
import
org.example.summers.SummerCallable
;
import
org.example.summers.SummerThread
;
import
org.example.worker.WorkPartitioner
;
public
class
ParallelSummer3
{
public
static
long
sum
(
int
[]
data
,
int
threadCount
)
{
/*
The sum function is used to distribute the task among threads for the completion of the mission.
The result of this operation is the count of prime numbers.
Note that the function use SummerCallable which it's run methods return future,
so you have to use for loop to get the result.
*/
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
threadCount
);
List
<
SummerCallable
>
summers
=
new
ArrayList
<
SummerCallable
>();
List
<
WorkPartitioner
.
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
for
(
WorkPartitioner
.
Part
part
:
parts
)
{
summers
.
add
(
new
SummerCallable
(
data
,
part
));
}
List
<
Future
<
Long
>>
results
;
try
{
results
=
executor
.
invokeAll
(
summers
);
}
catch
(
InterruptedException
e
)
{
System
.
err
.
println
(
"Cannot invoke the threads."
);
return
-
1
;
}
executor
.
shutdown
();
long
sum
=
0
;
for
(
Future
<
Long
>
future
:
results
)
{
try
{
sum
+=
future
.
get
();
}
catch
(
InterruptedException
|
ExecutionException
e
)
{
System
.
err
.
println
(
"Cannot get the results from threads."
);
return
-
2
;
}
}
System
.
out
.
println
(
"summer3 "
+
sum
);
return
sum
;
}
}
src/main/java/org/example/parallelsummers/ParallelSummer4.java
0 → 100644
View file @
d57be0bb
package
org
.
example
.
parallelsummers
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
import
org.example.summers.SummerCallable
;
import
org.example.summers.SummerThread
;
import
org.example.worker.WorkPartitioner
;
public
class
ParallelSummer4
{
public
static
long
sum
(
int
[]
data
,
int
threadCount
)
{
/*
The sum function is used to distribute the task among threads for the completion of the mission.
The result of this operation is the count of prime numbers.
Note that the function use SummerCallable which it's run methods return future,
so you have to use for loop to get the result.
*/
ExecutorService
executor
=
Executors
.
newFixedThreadPool
(
threadCount
);
List
<
Future
<
Long
>>
results
=
new
ArrayList
<
Future
<
Long
>>();
List
<
WorkPartitioner
.
Part
>
parts
=
WorkPartitioner
.
partitions
(
data
.
length
,
threadCount
);
for
(
WorkPartitioner
.
Part
part
:
parts
)
{
results
.
add
(
executor
.
submit
(
new
SummerCallable
(
data
,
part
)));
}
executor
.
shutdown
();
while
(!
executor
.
isTerminated
())
{
}
long
sum
=
0
;
for
(
Future
<
Long
>
future
:
results
)
{
try
{
sum
+=
future
.
get
();
}
catch
(
InterruptedException
|
ExecutionException
e
)
{
System
.
err
.
println
(
"Cannot get the results from threads."
);
return
-
2
;
}
}
System
.
out
.
println
(
"summer4 "
+
sum
);
return
sum
;
}
}
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