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
6456a84a
You need to sign in or sign up before continuing.
Commit
6456a84a
authored
Nov 01, 2023
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Callable vs Runnable
parent
d889e054
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
0 deletions
+67
-0
SubmitCallable.java
src/main/java/SubmitCallable.java
+33
-0
SubmitRunnable.java
src/main/java/SubmitRunnable.java
+34
-0
No files found.
src/main/java/SubmitCallable.java
0 → 100644
View file @
6456a84a
import
java.util.concurrent.*
;
public
class
SubmitCallable
{
public
static
void
main
(
String
arg
[]){
ExecutorService
executorService
=
Executors
.
newSingleThreadExecutor
();
Future
future
=
executorService
.
submit
(
newRunnable
(
" Task"
));
//not blocked
System
.
out
.
println
(
future
.
isDone
());
try
{
String
msg
=
(
String
)
future
.
get
();
//blocked
System
.
out
.
println
(
msg
);
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
ExecutionException
e
)
{
throw
new
RuntimeException
(
e
);
}
System
.
out
.
println
(
future
.
isDone
());
executorService
.
shutdown
();
}
private
static
Callable
newRunnable
(
String
msg
){
return
new
Callable
()
{
@Override
public
Object
call
()
throws
Exception
{
String
report
=
Thread
.
currentThread
().
getName
()+
" :"
+
msg
;
return
report
;
}
};
}
}
src/main/java/SubmitRunnable.java
0 → 100644
View file @
6456a84a
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
public
class
SubmitRunnable
{
public
static
void
main
(
String
arg
[]){
ExecutorService
executorService
=
Executors
.
newSingleThreadExecutor
();
Future
future
=
executorService
.
submit
(
newRunnable
(
" Task"
));
//not blocked
System
.
out
.
println
(
future
.
isDone
());
try
{
future
.
get
();
//blocked
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
ExecutionException
e
)
{
throw
new
RuntimeException
(
e
);
}
System
.
out
.
println
(
future
.
isDone
());
executorService
.
shutdown
();
}
private
static
Runnable
newRunnable
(
String
msg
){
return
new
Runnable
()
{
@Override
public
void
run
()
{
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