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
4f91f30f
Commit
4f91f30f
authored
Mar 06, 2023
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
02. Save Records in Parallel
parent
0995dcb6
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
92 additions
and
2 deletions
+92
-2
StudentService.java
src/main/java/Service/StudentService.java
+1
-0
StudentThread.java
src/main/java/StudentThread.java
+19
-0
Threads.java
src/main/java/Threads.java
+0
-2
ThreadTest.java
src/test/java/ThreadTest.java
+72
-0
No files found.
src/main/java/Service/StudentService.java
View file @
4f91f30f
...
...
@@ -12,6 +12,7 @@ public class StudentService {
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
System
.
out
.
println
(
"Student "
+
std
.
getName
()+
" is saved."
);
return
UUID
.
randomUUID
().
toString
();
}
}
src/main/java/StudentThread.java
0 → 100644
View file @
4f91f30f
import
Resource.Student
;
import
Service.StudentService
;
public
class
StudentThread
extends
Thread
{
private
Student
student
;
private
StudentService
studentService
;
public
StudentThread
(
Student
student
,
StudentService
studentService
)
{
this
.
student
=
student
;
this
.
studentService
=
studentService
;
}
@Override
public
void
run
()
{
System
.
out
.
println
(
"Name of current Thread: "
+
Thread
.
currentThread
().
getName
());
studentService
.
saveStudent
(
student
);
}
}
src/main/java/Threads.java
deleted
100644 → 0
View file @
0995dcb6
public
class
Threads
{
}
src/test/java/ThreadTest.java
View file @
4f91f30f
...
...
@@ -2,6 +2,9 @@ import Resource.Student;
import
Service.StudentService
;
import
junit.framework.TestCase
;
import
java.util.ArrayList
;
import
java.util.List
;
public
class
ThreadTest
extends
TestCase
{
public
void
testStudent
()
{
...
...
@@ -17,4 +20,73 @@ public class ThreadTest extends TestCase {
System
.
out
.
println
(
"Execution Time: "
+
execTime
+
" ms"
);
}
public
void
testStudentThread
()
{
long
start
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
"Name of current Thread: "
+
Thread
.
currentThread
().
getName
());
StudentService
studentService
=
new
StudentService
();
Student
std
=
new
Student
(
"student"
);
StudentThread
studentThread
=
new
StudentThread
(
std
,
studentService
);
studentThread
.
setName
(
"myThread"
);
studentThread
.
start
();
// start thread
// studentThread.run(); // treat as a simple object not as a thread
try
{
studentThread
.
join
();
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
long
execTime
=
System
.
currentTimeMillis
()
-
start
;
System
.
out
.
println
(
"Execution Time: "
+
execTime
+
" ms"
);
}
public
void
testStudentsThreads
(){
long
start
=
System
.
currentTimeMillis
();
StudentService
studentService
=
new
StudentService
();
for
(
int
i
=
1
;
i
<=
10
;++
i
){
Student
std
=
new
Student
(
"student "
+
i
);
StudentThread
studentThread
=
new
StudentThread
(
std
,
studentService
);
studentThread
.
setName
(
"myThread-"
+
i
);
studentThread
.
start
();
// start thread
// studentThread.run(); // treat as a simple object not as a thread
try
{
studentThread
.
join
();
//wait thread to complete
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
long
execTime
=
System
.
currentTimeMillis
()
-
start
;
System
.
out
.
println
(
"Execution Time: "
+
execTime
+
" ms"
);
}
public
void
testStudentThreadsPP
(){
long
start
=
System
.
currentTimeMillis
();
StudentService
studentService
=
new
StudentService
();
List
<
StudentThread
>
studentThreads
=
new
ArrayList
<>();
for
(
int
i
=
1
;
i
<=
10
;++
i
){
Student
std
=
new
Student
(
"student "
+
i
);
StudentThread
studentThread
=
new
StudentThread
(
std
,
studentService
);
studentThread
.
setName
(
"myThread-"
+
i
);
studentThread
.
start
();
// start thread
studentThreads
.
add
(
studentThread
);
// studentThread.run(); // treat as a simple object not as a thread
}
studentThreads
.
forEach
(
e
->
{
try
{
e
.
join
();
//wait thread to complete
}
catch
(
InterruptedException
ex
)
{
throw
new
RuntimeException
(
ex
);
}
});
long
execTime
=
System
.
currentTimeMillis
()
-
start
;
System
.
out
.
println
(
"Execution Time: "
+
execTime
+
" ms"
);
}
}
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