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
mohamadbashar.disoki
ForkJoin
Commits
ce1f11bb
Commit
ce1f11bb
authored
Mar 08, 2023
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
06. Fibonacci Fork/Join
parent
2c4c5e9f
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
0 deletions
+65
-0
README.md
README.md
+3
-0
Fibonacci.java
src/main/java/Fibonacci.java
+37
-0
FibonacciTest.java
src/test/java/FibonacciTest.java
+25
-0
No files found.
README.md
View file @
ce1f11bb
...
...
@@ -13,8 +13,11 @@
### Introduce to ForkJoin Framework
*
ArraySum using Recursive Action
*
Fibonacci Using Recursive Task
### Acknowledgments
Inspiration, code snippets, etc.
*
[
Java - MultiThreading
](
https://www.youtube.com/watch?v=9DvDheKRJ9Y&t=656s
)
*
[
Java - Executor Service
](
https://jenkov.com/tutorials/java-util-concurrent/executorservice.html
)
*
[
Recursive Task
](
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/RecursiveTask.html
)
*
[
Parallel Programming in Java
]
(
https://www.coursera.org/learn/parallel-programming-in-java
)
\ No newline at end of file
src/main/java/Fibonacci.java
0 → 100644
View file @
ce1f11bb
import
java.util.concurrent.RecursiveTask
;
/*
However, besides being a dumb way to compute Fibonacci functions (there is a simple fast linear algorithm that you'd use in practice),
this is likely to perform poorly because the smallest subtasks are too small to be worthwhile splitting up.
Instead, as is the case for nearly all fork/join applications,
you'd pick some minimum granularity size (for example 10 here) for which you always sequentially solve rather than subdividing.
*/
public
class
Fibonacci
extends
RecursiveTask
<
Integer
>
{
final
int
n
;
public
Fibonacci
(
int
n
)
{
this
.
n
=
n
;
}
public
Integer
compute
()
{
if
(
n
>
20
)
{
if
(
n
<=
1
)
return
n
;
Fibonacci
f1
=
new
Fibonacci
(
n
-
1
);
f1
.
fork
();
Fibonacci
f2
=
new
Fibonacci
(
n
-
2
);
return
f2
.
compute
()
+
f1
.
join
();
}
else
{
return
computeSeq
();
}
}
public
Integer
computeSeq
()
{
if
(
n
<=
1
)
return
n
;
Fibonacci
f1
=
new
Fibonacci
(
n
-
1
);
Fibonacci
f2
=
new
Fibonacci
(
n
-
2
);
return
f2
.
computeSeq
()
+
f1
.
computeSeq
();
}
}
src/test/java/FibonacciTest.java
0 → 100644
View file @
ce1f11bb
import
junit.framework.TestCase
;
public
class
FibonacciTest
extends
TestCase
{
int
n
=
40
;
public
void
testFiboPP
(){
long
start
=
System
.
currentTimeMillis
();
Fibonacci
fib
=
new
Fibonacci
(
n
);
int
res
=
fib
.
compute
();
long
end
=
System
.
currentTimeMillis
()-
start
;
System
.
out
.
printf
(
"Fibonacci for %d is %d, and parallel execution took %d ms\n"
,
n
,
res
,
end
);
// Fibonacci parallel takes 144269 fon n = 50 before enhancement 1
}
public
void
testFiboSeq
(){
long
start
=
System
.
currentTimeMillis
();
Fibonacci
fib
=
new
Fibonacci
(
n
);
int
res
=
fib
.
computeSeq
();
long
end
=
System
.
currentTimeMillis
()-
start
;
System
.
out
.
printf
(
"Fibonacci for %d is %d, and sequential execution took %d ms\n"
,
n
,
res
,
end
);
//Fibonacci Sequential takes 86495 fon n = 50 before enhancement 1
}
}
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