Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
P
PP-02-ExecutorService
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
PP-02-ExecutorService
Commits
3f281d5d
You need to sign in or sign up before continuing.
Commit
3f281d5d
authored
Nov 19, 2025
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
optimization and update
parent
f4a50188
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
47 additions
and
46 deletions
+47
-46
InvokeAll.java
src/main/java/InvokeAll.java
+24
-26
RestApiLoadTester.java
src/main/java/RestApiLoadTester.java
+4
-2
SubmitCallable.java
src/main/java/SubmitCallable.java
+3
-3
SubmitRunnable.java
src/main/java/SubmitRunnable.java
+16
-15
No files found.
src/main/java/InvokeAll.java
View file @
3f281d5d
import
java.util.Collections
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Set
;
...
...
@@ -8,37 +7,36 @@ public class InvokeAll {
public
static
void
main
(
String
[]
args
)
{
// Use a try-with-resources statement for the ExecutorService
// if you are using Java 19 or later.
// For earlier versions, a try-finally block is a good practice.
ExecutorService
executorService
=
Executors
.
newSingleThreadExecutor
();
Set
<
Callable
<
String
>>
callables
=
new
HashSet
<
Callable
<
String
>>();
try
{
Set
<
Callable
<
String
>>
callables
=
new
HashSet
<>();
callables
.
add
(
new
Callable
<
String
>()
{
public
String
call
()
throws
Exception
{
return
Thread
.
currentThread
().
getName
()
+
" Task 1"
;
}
});
callables
.
add
(
new
Callable
<
String
>()
{
public
String
call
()
throws
Exception
{
return
Thread
.
currentThread
().
getName
()
+
" Task 2"
;
}
});
callables
.
add
(
new
Callable
<
String
>()
{
public
String
call
()
throws
Exception
{
return
Thread
.
currentThread
().
getName
()
+
" Task 3"
;
}
});
// Use lambda expressions for concise callable implementations
callables
.
add
(()
->
Thread
.
currentThread
().
getName
()
+
" Task 1"
);
callables
.
add
(()
->
Thread
.
currentThread
().
getName
()
+
" Task 2"
);
callables
.
add
(()
->
Thread
.
currentThread
().
getName
()
+
" Task 3"
);
// invokeAll blocks until all tasks are complete
List
<
Future
<
String
>>
futures
=
executorService
.
invokeAll
(
callables
);
for
(
Future
<
String
>
future
:
futures
)
{
try
{
List
<
Future
<
String
>>
result
=
executorService
.
invokeAll
(
callables
);
//Once of task is fully executed the other waiting task will get canceled
for
(
Future
<
String
>
res
:
result
)
{
System
.
out
.
println
(
res
.
get
());
}
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
// get() will not block here because invokeAll has already completed.
System
.
out
.
println
(
future
.
get
());
}
catch
(
ExecutionException
e
)
{
throw
new
RuntimeException
(
e
);
// This is where exceptions from the callables are thrown
e
.
printStackTrace
();
}
}
}
catch
(
InterruptedException
e
)
{
// This is thrown if the thread is interrupted while waiting on invokeAll
Thread
.
currentThread
().
interrupt
();
e
.
printStackTrace
();
}
finally
{
executorService
.
shutdown
();
}
}
}
src/main/java/RestApiLoadTester.java
View file @
3f281d5d
...
...
@@ -7,10 +7,12 @@ import okhttp3.*;
public
class
RestApiLoadTester
{
public
static
void
main
(
String
[]
args
){
String
url
=
"http://172.29.3.203:30152/hello/fib/30"
;
// String url = "http://172.29.3.203:30152/hello/fib/30";
String
url
=
"http://localhost:8080/test"
;
String
headers
=
"content-type: application/json\n"
+
"user-agent: Mozilla/5.0"
;
int
threadCount
=
5
;
int
threadCount
=
10
;
int
callCountPerThread
=
2
;
RestApiLoadTester
.
runLoadTest
(
url
,
headers
,
threadCount
,
callCountPerThread
);
...
...
src/main/java/SubmitCallable.java
View file @
3f281d5d
...
...
@@ -4,7 +4,7 @@ public class SubmitCallable {
public
static
void
main
(
String
arg
[]){
ExecutorService
executorService
=
Executors
.
newSingleThreadExecutor
();
Future
future
=
executorService
.
submit
(
new
Runn
able
(
" Task"
));
//not blocked
Future
future
=
executorService
.
submit
(
new
Call
able
(
" Task"
));
//not blocked
System
.
out
.
println
(
future
.
isDone
());
try
{
...
...
@@ -19,7 +19,7 @@ public class SubmitCallable {
executorService
.
shutdown
();
}
private
static
Callable
new
Runn
able
(
String
msg
){
private
static
Callable
new
Call
able
(
String
msg
){
return
new
Callable
()
{
@Override
public
Object
call
()
throws
Exception
{
...
...
src/main/java/SubmitRunnable.java
View file @
3f281d5d
...
...
@@ -4,14 +4,14 @@ import java.util.concurrent.Executors;
import
java.util.concurrent.Future
;
public
class
SubmitRunnable
{
public
static
void
main
(
String
arg
[]){
public
static
void
main
(
String
arg
[])
{
ExecutorService
executorService
=
Executors
.
newSingleThreadExecutor
();
Future
future
=
executorService
.
submit
(
newRunnable
(
" Task"
));
//not blocked
Future
future
=
executorService
.
submit
(
newRunnable
(
" Task"
));
//not blocked
System
.
out
.
println
(
future
.
isDone
());
try
{
future
.
get
();
//blocked
future
.
get
();
//blocked if the task not finished yet
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
ExecutionException
e
)
{
...
...
@@ -19,13 +19,14 @@ public class SubmitRunnable {
}
System
.
out
.
println
(
future
.
isDone
());
executorService
.
shutdown
();
//
executorService.shutdown();
}
private
static
Runnable
newRunnable
(
String
msg
){
private
static
Runnable
newRunnable
(
String
msg
)
{
return
new
Runnable
()
{
@Override
public
void
run
()
{
String
report
=
Thread
.
currentThread
().
getName
()
+
" :"
+
msg
;
String
report
=
Thread
.
currentThread
().
getName
()
+
" :"
+
msg
;
System
.
out
.
println
(
report
);
}
};
...
...
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