The Java ExecutorService Interface (Parts 1, 2, and 3)

preview_player
Показать описание
This video gives an overview of the structure and functionality of the Java Executor Service interface, as well as the key capabilities of the Java ThreadPoolExecutor implementation of this interface.
Рекомендации по теме
Комментарии
Автор

0:20 overview of ExecutorService
0:24 extends Executor to submit tasks
1:03 analogy of Future
1:47 ExecutorService relies on bunch of things
2:06 Runnable
2:16 Callable
2:44 we typically use Callable to run task asynchronously
3:36 under the hood, Callable implements the Active Object pattern
4:34 Future
5:43 analogy: Wendy's
5:49 they will take your order, then rather having you waiting in the queue synchronously (block), they will go off and cook your meal
6:12 you get a table tent number, then go off and do anything you want, 6:21 then when the food is ready, there is a couple different ways to get it
7:27 can be canceled, tested to see if task is done
8:10 Other Future variants implement ExecutorService
8:20 FutureTask
8:36 RunnableFuture
9:04 CompletableFuture
9:34
10:15 part 2
10:28 the ExecutorService can execute individual tasks
10:34 you can execute a Runnable by using execute()
10:51 submit
11:12 support "synchronous future" processing model
11:31 Future.get() blocks until task completed successfully
12:04 ExecutorService can also execute group of tasks – invoke
12:32 both invokeAll and invokeAny are squirelly in that they actually *block until done* => a bit non-intuitive
13:19 invokeAll
13:40 invokeAny
14:24 how to shut things down
14:35 control the life cycle of the ExecutorService per se
14:45 shutdown provides an *"orderly shutdown"* that completes existing tasks
15:02 question: why don't we want to just put the battery out of a laptop
15:12 there are some states in memory that has been flushed to persistent backing store (database)
16:05 shutdownNow
16:21 if there are tasks not run yet,
16:34 ExecutorService can query ExecutorService instance to see what status in
16:42 boolean isShutdown()
16:47 boolean isTerminated()
16:56 awaitTermination() - block until all tasks complete
17:17 analogy - think of it as a barrier synchronizer
17:40 part 3: 17:43 How the ThreadPoolExecutor implements ExecutorService
17:55 ThreadPoolExecutor implements ExecutorService indirectly via AbstractExecutorService sub-class
18:49 What ThreadPoolExecutor does internally 18:51 it has a blocking queue that keeps track of tasks passed to the ThreadPoolExecutor
19:22 the queue type can be strategized
19:35 SynchronousQueue - 19:38 support direct handoff
19:57 benefit: avoid deadlock
20:01 downside: you can create a lot of threads

ruixue