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
75718739
Commit
75718739
authored
Nov 01, 2023
by
Mohamad Bashar Desoki
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
InvokeAll vs InvokeAny
parent
6456a84a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
89 additions
and
0 deletions
+89
-0
InvokeAll.java
src/main/java/InvokeAll.java
+44
-0
InvokeAny.java
src/main/java/InvokeAny.java
+45
-0
No files found.
src/main/java/InvokeAll.java
0 → 100644
View file @
75718739
import
java.util.Collections
;
import
java.util.HashSet
;
import
java.util.List
;
import
java.util.Set
;
import
java.util.concurrent.*
;
public
class
InvokeAll
{
public
static
void
main
(
String
[]
args
)
{
ExecutorService
executorService
=
Executors
.
newSingleThreadExecutor
();
Set
<
Callable
<
String
>>
callables
=
new
HashSet
<
Callable
<
String
>>();
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"
;
}
});
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
);
}
catch
(
ExecutionException
e
)
{
throw
new
RuntimeException
(
e
);
}
executorService
.
shutdown
();
}
}
src/main/java/InvokeAny.java
0 → 100644
View file @
75718739
import
java.util.HashSet
;
import
java.util.Set
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.ExecutionException
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
public
class
InvokeAny
{
public
static
void
main
(
String
[]
args
)
{
ExecutorService
executorService
=
Executors
.
newSingleThreadExecutor
();
Set
<
Callable
<
String
>>
callables
=
new
HashSet
<
Callable
<
String
>>();
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"
;
}
});
try
{
String
result
=
executorService
.
invokeAny
(
callables
);
//Once of task is fully executed the other waiting task will get canceled
System
.
out
.
println
(
result
);
}
catch
(
InterruptedException
e
)
{
throw
new
RuntimeException
(
e
);
}
catch
(
ExecutionException
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