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
16af0fae
Commit
16af0fae
authored
Mar 07, 2023
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
04. Save Records using ExecutorService
parent
9f6b67c2
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
93 additions
and
1 deletion
+93
-1
README.md
README.md
+6
-1
ExecutorServiceTest.java
src/test/java/ExecutorServiceTest.java
+87
-0
No files found.
README.md
View file @
16af0fae
...
@@ -3,5 +3,10 @@
...
@@ -3,5 +3,10 @@
## Review Threads in Java
## Review Threads in Java
### Why we need threads?
### Why we need threads?
Save 10 Records sequentially as Example.
<li>
Save 10 Records sequentially as Example.
</li>
<li>
Save Records using new Thread for each request!
</li>
### Why we need StudentExecutor Framework?
<li>
Discover the limitless thread creation
</li>
<li>
Create FixedThread pool
</li>
<li>
Create Scheduled Thread pool
</li>
src/test/java/ExecutorServiceTest.java
0 → 100644
View file @
16af0fae
/*
Ref:
https://www.youtube.com/watch?v=9DvDheKRJ9Y&t=656s
https://jenkov.com/tutorials/java-util-concurrent/executorservice.html
Threads disadvantages and limitation
1- Thread Creation and teardown comes with a cost, both in terms of compute and time, it adds latency into request processing
2- Number of threads that can be created is limited
3- When there are more threads than the number of processors or cores, then the thread sit idle (memory effect).
ExecutorService automatically provides a pool of threads and an API for assigning tasks to it, to fix this issue.
it's based on the producer-consumer pattern
All thread in the pool are managed by a queue
some Thread pool type:
newFixedThreadPool
newCachedThreadPool
newSingleThreadExecutor
newScheduledThreadPool
* */
import
Resource.Student
;
import
Service.StudentService
;
import
junit.framework.TestCase
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.concurrent.*
;
public
class
ExecutorServiceTest
extends
TestCase
{
public
void
testFixedThreadPool
()
{
StudentService
studentService
=
new
StudentService
();
ExecutorService
executorService
=
Executors
.
newFixedThreadPool
(
5
);
List
<
Future
<
String
>>
futures
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
30
;
++
i
)
{
Student
std
=
new
Student
(
"student "
+
i
);
Future
<
String
>
future
=
executorService
.
submit
(()
->
studentService
.
saveStudent
(
std
));
futures
.
add
(
future
);
}
futures
.
forEach
(
e
->
{
try
{
System
.
out
.
println
(
e
.
get
());
}
catch
(
InterruptedException
ex
)
{
throw
new
RuntimeException
(
ex
);
}
catch
(
ExecutionException
ex
)
{
throw
new
RuntimeException
(
ex
);
}
});
// try {
// executorService.awaitTermination(5,TimeUnit.SECONDS);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// while (!executorService.isTerminated()){
// try {
// System.out.println("Terminating..");
// Thread.sleep(500);
// } catch (InterruptedException e) {
// throw new RuntimeException(e);
// }
// }
}
public
void
testScheduledThreadPool
()
{
StudentService
studentService
=
new
StudentService
();
ScheduledExecutorService
executorService
=
Executors
.
newScheduledThreadPool
(
5
);
Student
std
=
new
Student
(
"student"
);
// executorService.scheduleAtFixedRate(() -> System.out.println("hello"), 1, 3, TimeUnit.SECONDS);
executorService
.
scheduleAtFixedRate
(()
->
studentService
.
saveStudent
(
std
),
1
,
3
,
TimeUnit
.
SECONDS
);
try
{
executorService
.
awaitTermination
(
15
,
TimeUnit
.
SECONDS
);
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
executorService
.
shutdown
();
}
}
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