From the course: Advanced Java: Threads and Concurrency
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Implementing an executor: ExecutorService in action - Java Tutorial
From the course: Advanced Java: Threads and Concurrency
Implementing an executor: ExecutorService in action
- [Instructor] Let's see ExecutorService in action now. The first step is to create an ExecutorService. There are a couple of ways to do this, but the easiest way is to use one of the factory methods of the java.util.concurrent.executors class. In line seven of ExecutorDemoOne, I will use the newSingleThreadExecutor method that takes an executor, that uses a single worker thread to execute tasks. This is useful when we want to ensure that tasks are executed in the order in which they are submitted. It is the same as using newFixedThreadPool one. In this case, it doesn't really make any difference if you use either way. I just wanted to show you that both methods exist. In line nine, I call the execute method, passing it the runnable task. My task is simply to print a statement. And finally, in line 16, I shutdown the ExecutorService as I'm done with executing my task. Upon running, I get this output, "pool-1-thread-1 executing the task." pool-1-thread-1 is the name of the thread which…
Contents
-
-
-
-
-
(Locked)
Managing thread execution: Thread pools and executors4m 3s
-
(Locked)
ExecutorService3m 55s
-
(Locked)
Implementing an executor: ExecutorService in action5m 48s
-
(Locked)
Executing tasks periodically with ScheduledExecutorService6m 22s
-
(Locked)
Challenge: Number magic and voting simulator2m 20s
-
(Locked)
Solution: Number magic and voting simulator5m 41s
-
(Locked)
-
-
-
-
-