Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
F
ForkJoin
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
nassouh.alolabi
ForkJoin
Commits
2c4c5e9f
Commit
2c4c5e9f
authored
Mar 08, 2023
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
05. ArraySum Fork/Join
parent
5edc7e7b
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
128 additions
and
0 deletions
+128
-0
README.md
README.md
+3
-0
ArraySum.java
src/main/java/ArraySum.java
+53
-0
ArraySumTest.java
src/test/java/ArraySumTest.java
+72
-0
No files found.
README.md
View file @
2c4c5e9f
...
@@ -11,6 +11,9 @@
...
@@ -11,6 +11,9 @@
*
Create FixedThreadPool
*
Create FixedThreadPool
*
Create ScheduledThreadPool
*
Create ScheduledThreadPool
### Introduce to ForkJoin Framework
*
ArraySum using Recursive Action
### Acknowledgments
### Acknowledgments
Inspiration, code snippets, etc.
Inspiration, code snippets, etc.
*
[
Java - MultiThreading
](
https://www.youtube.com/watch?v=9DvDheKRJ9Y&t=656s
)
*
[
Java - MultiThreading
](
https://www.youtube.com/watch?v=9DvDheKRJ9Y&t=656s
)
...
...
src/main/java/ArraySum.java
0 → 100644
View file @
2c4c5e9f
import
java.util.Arrays
;
import
java.util.concurrent.RecursiveAction
;
public
class
ArraySum
extends
RecursiveAction
{
public
long
sum
;
int
arr
[],
lo
,
hi
;
public
ArraySum
(
int
[]
arr
,
int
lo
,
int
hi
)
{
this
.
arr
=
arr
;
this
.
lo
=
lo
;
this
.
hi
=
hi
;
}
public
long
computeSeq
()
{
// if(lo == hi)
// return arr[lo];
// else {
// int mid = (lo + hi) / 2;
// ArraySum left = new ArraySum(arr, lo, mid);
// ArraySum right = new ArraySum(arr, mid + 1, hi);
//
// return left.computeSeq()+ right.computeSeq();
// }
for
(
int
i
=
lo
;
i
<=
hi
;
++
i
)
{
sum
+=
arr
[
i
];
}
return
sum
;
}
@Override
protected
void
compute
()
{
if
(
hi
-
lo
>
1_000_000
)
{
int
mid
=
(
lo
+
hi
)
/
2
;
ArraySum
left
=
new
ArraySum
(
arr
,
lo
,
mid
);
ArraySum
right
=
new
ArraySum
(
arr
,
mid
+
1
,
hi
);
left
.
fork
();
right
.
compute
();
left
.
join
();
sum
=
left
.
sum
+
right
.
sum
;
}
else
{
// sum =computeSeq();
for
(
int
i
=
lo
;
i
<=
hi
;
++
i
)
{
sum
+=
arr
[
i
];
}
}
}
public
void
computeStream
()
{
sum
=
Arrays
.
stream
(
arr
).
asLongStream
().
parallel
().
sum
();
// all the intermediate and final operation run in parallel
// Introduce to Map Reduce pattern credit to java streams
}
}
src/test/java/ArraySumTest.java
0 → 100644
View file @
2c4c5e9f
import
junit.framework.TestCase
;
import
java.util.Random
;
import
java.util.concurrent.ForkJoinPool
;
import
java.util.stream.IntStream
;
public
class
ArraySumTest
extends
TestCase
{
private
int
[]
randomArray
(
int
size
)
{
Random
rand
=
new
Random
();
int
[]
arr
=
new
int
[
size
];
for
(
int
i
=
0
;
i
<
size
;
i
++)
{
arr
[
i
]
=
rand
.
nextInt
(
size
);
}
/* Check random array
Arrays.stream(arr).forEach(System.out::println);*/
return
arr
;
}
private
int
[]
intStream
(
int
size
)
{
IntStream
intStream
=
IntStream
.
range
(
1
,
size
);
return
intStream
.
toArray
();
}
public
void
testArraySumSeq
()
{
int
size
=
1000_000_000
;
int
[]
arr
=
intStream
(
size
);
ArraySum
array
=
new
ArraySum
(
arr
,
0
,
arr
.
length
-
1
);
long
start
=
System
.
currentTimeMillis
();
long
sum
=
array
.
computeSeq
();
long
endTimer
=
System
.
currentTimeMillis
()
-
start
;
System
.
out
.
printf
(
"Sequential Time execution for Random Array of size %d is %d sum is %d\n"
,
size
,
endTimer
,
sum
);
// assertEquals(15,res);
}
public
void
testArraySumPP
()
{
System
.
setProperty
(
"java.util.concurrent.ForkJoinPool.common.parallelism"
,
"7"
);
int
size
=
1000_000_000
;
int
[]
arr
=
intStream
(
size
);
ArraySum
array
=
new
ArraySum
(
arr
,
0
,
arr
.
length
-
1
);
long
start
=
System
.
currentTimeMillis
();
ForkJoinPool
.
commonPool
().
invoke
(
array
);
long
endTimer
=
System
.
currentTimeMillis
()
-
start
;
System
.
out
.
printf
(
"Parallel Time execution for Random Array of size %d is %d ms sum is %d\n"
,
size
,
endTimer
,
array
.
sum
);
// assertEquals(15,res);
}
public
void
testArraySumStream
()
{
System
.
setProperty
(
"java.util.concurrent.ForkJoinPool.common.parallelism"
,
"7"
);
int
size
=
1000_000_000
;
int
[]
arr
=
intStream
(
size
);
ArraySum
array
=
new
ArraySum
(
arr
,
0
,
arr
.
length
-
1
);
long
start
=
System
.
currentTimeMillis
();
array
.
computeStream
();
long
endTimer
=
System
.
currentTimeMillis
()
-
start
;
System
.
out
.
printf
(
"Parallel Stream Time execution for Random Array of size %d is %d ms sum is %d\n"
,
size
,
endTimer
,
array
.
sum
);
// assertEquals(15,res);
}
public
void
resource
()
{
System
.
out
.
println
(
Runtime
.
getRuntime
().
availableProcessors
());
System
.
out
.
println
(
ForkJoinPool
.
commonPool
().
getParallelism
());
}
}
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