The document discusses the use of thread pools in Java to manage thread creation efficiently, highlighting various types of executors such as CachedThreadPool, FixedThreadPool, and ScheduledThreadPool. It explains how to submit tasks using the ExecutorService interface and manage task execution with Future objects. Additionally, it outlines the shutdown process for thread pools and provides code examples for creating and using different types of executors.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
9 views11 pages
Executors
The document discusses the use of thread pools in Java to manage thread creation efficiently, highlighting various types of executors such as CachedThreadPool, FixedThreadPool, and ScheduledThreadPool. It explains how to submit tasks using the ExecutorService interface and manage task execution with Future objects. Additionally, it outlines the shutdown process for thread pools and provides code examples for creating and using different types of executors.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11
Executors
• Constructing a new thread is somewhat expensive because it involves
interaction with the operating system. • If your program creates a large number of short-lived threads, it should use a thread pool instead. • A thread pool contains a number of idle threads that are ready to run. • Give a Runnable to the pool, and one of the threads calls the run method. • When the run method exits, the thread doesn’t die but stays around to serve the next request. • The Executors class has a number of static factory methods for constructing thread pools • NewCachedThreadPool • New threads are created as needed; idle threads are kept for 60 seconds. • New FixedThreadPool • The pool contains a fixed set of threads; idle threads are kept indefinitely. • New SingleThreadExecutor • A “pool” with a single thread that executes the submitted tasks sequentially (similar to the Swing event dispatch thread). • New ScheduledThreadPool • A fixed-thread pool for scheduled execution; a replacement for java.util.Timer. • New SingleThreadScheduledExecutor • A single-thread “pool” for scheduled execution. • newCachedThreadPool • constructs a thread pool that executes each task immediately, using an existing idle thread when available and creating a new thread otherwise. • newFixedThreadPool • Constructs a thread pool with a fixed size. If more tasks are submitted than there are idle threads, the unserved tasks are placed on a queue. • They are run when other tasks have completed. • newSingleThreadExecutor • is a degenerate pool of size 1 where a single thread executes the submitted tasks, one after another. • These three methods return an object of the ThreadPoolExecutor class that implements the ExecutorService interface • submit a Runnable or Callable to an ExecutorService with one of the following methods: • Future<?> submit(Runnable task) • returns an odd-looking Future<?>. You can use such an object to call isDone, cancel, or isCancelled, • Future<T> submit(Runnable task, T result) • submits a Runnable, and the get method of the Future returns the given result object upon completion • Future<T> submit(Callable<T> task) • submits a Callable, and the returned Future gets the result of the computation when it is ready. • The pool will run the submitted task at its earliest convenience. • When you call submit, you get back a Future object that you can use to query the state of the task. • When you are done with a thread pool, call shutdown. • This method initiates the shutdown sequence for the pool. • An executor that is shut down accepts no new tasks. • When all tasks are finished, the threads in the pool die. Alternatively, you can call shutdownNow. • The pool then cancels all tasks that have not yet begun and attempts to interrupt the running threads Sequence for ThreadExecutors • 1. Call the static newCachedThreadPool or newFixedThreadPool method of the Executors class. • 2. Call submit to submit Runnable or Callable objects. • 3. If you want to be able to cancel a task, or if you submit Callable objects, hang on to the returned Future objects. • 4. Call shutdown when you no longer want to submit any tasks. • ExecutorService executorService1 = Executors.newSingleThreadExecutor();
• ExecutorService executorService = Executors.newFixedThreadPool(10); • executorService.execute(new Runnable() { • public void run() { • System.out.println("Asynchronous task"); • } • }); • executorService.shutdown(); • ExecutorService is created using the Executors newFixedThreadPool() factory method. This creates a thread pool with 10 threads executing tasks. • An anonymous implementation of the Runnable interface is passed to the execute() method. • This causes the Runnable to be executed by one of the threads in the ExecutorService. Fixed thread pool executor
Creates a thread pool that reuses a fixed number of threads to
execute any number of tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available.
Executors.newFixedThreadPool(10); ScheduledThreadPoolExecutor • Fixed thread pools or cached thread pools are good when you have to execute one unique task only once. • When you need to execute a task, repeatedly N times, either N fixed number of times or infinitively after fixed delay, you should be using ScheduledThreadPoolExecutor. • ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) – • Creates and executes a periodic action that becomes enabled first after the given initial delay, and subsequently with the given delay period. • No matter how much time a long running task takes, there will be a fixed delay time gap between two executions.