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
9f6b67c2
Commit
9f6b67c2
authored
Mar 07, 2023
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
03. Save Record using Runnable
parent
4f91f30f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
81 additions
and
9 deletions
+81
-9
StudentRunnable.java
src/main/java/StudentRunnable.java
+19
-0
ThreadTest.java
src/test/java/ThreadTest.java
+62
-9
No files found.
src/main/java/StudentRunnable.java
0 → 100644
View file @
9f6b67c2
import
Resource.Student
;
import
Service.StudentService
;
public
class
StudentRunnable
implements
Runnable
{
private
Student
student
;
private
StudentService
studentService
;
public
StudentRunnable
(
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/test/java/ThreadTest.java
View file @
9f6b67c2
...
@@ -24,7 +24,7 @@ public class ThreadTest extends TestCase {
...
@@ -24,7 +24,7 @@ public class ThreadTest extends TestCase {
public
void
testStudentThread
()
{
public
void
testStudentThread
()
{
long
start
=
System
.
currentTimeMillis
();
long
start
=
System
.
currentTimeMillis
();
System
.
out
.
println
(
"Name of current Thread: "
+
Thread
.
currentThread
().
getName
());
System
.
out
.
println
(
"Name of current Thread: "
+
Thread
.
currentThread
().
getName
());
StudentService
studentService
=
new
StudentService
();
StudentService
studentService
=
new
StudentService
();
Student
std
=
new
Student
(
"student"
);
Student
std
=
new
Student
(
"student"
);
...
@@ -42,16 +42,16 @@ public class ThreadTest extends TestCase {
...
@@ -42,16 +42,16 @@ public class ThreadTest extends TestCase {
System
.
out
.
println
(
"Execution Time: "
+
execTime
+
" ms"
);
System
.
out
.
println
(
"Execution Time: "
+
execTime
+
" ms"
);
}
}
public
void
testStudentsThreads
(){
public
void
testStudentsThreads
()
{
long
start
=
System
.
currentTimeMillis
();
long
start
=
System
.
currentTimeMillis
();
StudentService
studentService
=
new
StudentService
();
StudentService
studentService
=
new
StudentService
();
for
(
int
i
=
1
;
i
<=
10
;++
i
)
{
for
(
int
i
=
1
;
i
<=
10
;
++
i
)
{
Student
std
=
new
Student
(
"student "
+
i
);
Student
std
=
new
Student
(
"student "
+
i
);
StudentThread
studentThread
=
new
StudentThread
(
std
,
studentService
);
StudentThread
studentThread
=
new
StudentThread
(
std
,
studentService
);
studentThread
.
setName
(
"myThread-"
+
i
);
studentThread
.
setName
(
"myThread-"
+
i
);
studentThread
.
start
();
// start thread
studentThread
.
start
();
// start thread
// studentThread.run(); // treat as a simple object not as a thread
// studentThread.run(); // treat as a simple object not as a thread
try
{
try
{
...
@@ -64,16 +64,16 @@ public class ThreadTest extends TestCase {
...
@@ -64,16 +64,16 @@ public class ThreadTest extends TestCase {
System
.
out
.
println
(
"Execution Time: "
+
execTime
+
" ms"
);
System
.
out
.
println
(
"Execution Time: "
+
execTime
+
" ms"
);
}
}
public
void
testStudentThreadsPP
(){
public
void
testStudentThreadsPP
()
{
long
start
=
System
.
currentTimeMillis
();
long
start
=
System
.
currentTimeMillis
();
StudentService
studentService
=
new
StudentService
();
StudentService
studentService
=
new
StudentService
();
List
<
StudentThread
>
studentThreads
=
new
ArrayList
<>();
List
<
StudentThread
>
studentThreads
=
new
ArrayList
<>();
for
(
int
i
=
1
;
i
<=
10
;++
i
)
{
for
(
int
i
=
1
;
i
<=
10
;
++
i
)
{
Student
std
=
new
Student
(
"student "
+
i
);
Student
std
=
new
Student
(
"student "
+
i
);
StudentThread
studentThread
=
new
StudentThread
(
std
,
studentService
);
StudentThread
studentThread
=
new
StudentThread
(
std
,
studentService
);
studentThread
.
setName
(
"myThread-"
+
i
);
studentThread
.
setName
(
"myThread-"
+
i
);
studentThread
.
start
();
// start thread
studentThread
.
start
();
// start thread
studentThreads
.
add
(
studentThread
);
studentThreads
.
add
(
studentThread
);
// studentThread.run(); // treat as a simple object not as a thread
// studentThread.run(); // treat as a simple object not as a thread
...
@@ -89,4 +89,57 @@ public class ThreadTest extends TestCase {
...
@@ -89,4 +89,57 @@ public class ThreadTest extends TestCase {
long
execTime
=
System
.
currentTimeMillis
()
-
start
;
long
execTime
=
System
.
currentTimeMillis
()
-
start
;
System
.
out
.
println
(
"Execution Time: "
+
execTime
+
" ms"
);
System
.
out
.
println
(
"Execution Time: "
+
execTime
+
" ms"
);
}
}
public
void
testStudentRunnable
()
{
StudentService
studentService
=
new
StudentService
();
Student
std
=
new
Student
(
"student"
);
StudentRunnable
studentRunnable
=
new
StudentRunnable
(
std
,
studentService
);
Thread
thread
=
new
Thread
(
studentRunnable
);
thread
.
start
();
try
{
thread
.
join
();
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
System
.
out
.
println
(
"Completed Runnable"
);
}
public
void
testStudentRunnableAnonymousClass
()
{
StudentService
studentService
=
new
StudentService
();
Student
std
=
new
Student
(
"student"
);
// StudentRunnable studentRunnable = new StudentRunnable(std, studentService);
Thread
thread
=
new
Thread
(
new
Runnable
()
{
@Override
public
void
run
()
{
studentService
.
saveStudent
(
std
);
}
});
thread
.
start
();
try
{
thread
.
join
();
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
System
.
out
.
println
(
"Completed Runnable"
);
}
public
void
testStudentRunnableLambda
()
{
StudentService
studentService
=
new
StudentService
();
Student
std
=
new
Student
(
"student"
);
Thread
thread
=
new
Thread
(()
->
{
studentService
.
saveStudent
(
std
);
});
thread
.
start
();
try
{
thread
.
join
();
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
System
.
out
.
println
(
"Completed Runnable"
);
}
}
}
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