Download presentation
Presentation is loading. Please wait.
Published byMalcolm Houston Modified over 8 years ago
1
…in java DThread Pools x
2
Thread Pools: Why? Source code updates – copy/paste of run/runnable method for each thread is exhausting There is overhead to continually create & destroy threads Create thread, thread does work, Destroy thread. Repeat. Leads to better program design By delegating thread management to a pool manager (executor in javaspeak) If (#threads >#cpu’s) then there is greater program efficiency Don’t forget the jvm has its own threads too!
3
Thread Pools: Why Not? If it doesn’t matter, don’t create a thread pool Consider: batch processing or getting a simple answer is all that matters If there are enough cpu resources If (#threads is way way < #cpu’s) then there is greater program efficiency Using thread pools could do more harm than good You are demonstrating how to start a thread
4
Thread Pools: What (for)? Thread pools manage the threads for you A thread pool can throttle the total number of threads So threads do not overwhelm the system Thread pools allow you to reuse threads And avoid overhead of creating/destroying threads
5
Thread Pools: How? Think of a task being handled by a thread… Create tasks that each thread should run Create the pool of threads That’s it (!) Let’s take a look at the interface and some code!
6
Thread Pools: How (continued)? package java.util.concurrent; Public interface Executor { public void execute(Runnable task); } Model your code as a series of tasks No need to worry about thread details Create a task, and pass it to the execute method. Introducing: deadpool The executor!
7
Thread Pools: How (continued)? package java.util.concurrent; Public interface ExecutorService extends Executor { void shutdown(); boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException; Future submit(Runnable task); //ETC… } This interface allows you to manage both executor & tasks shutdown() gracefully terminated executor (no new tasks accepted) submit() /*there are several flavors*/ allows tasks to be submitted and tracked, via a Future object. Do read the textbooks’s java supplement on threads & see javadocs for more. Introducing: The ExecutorService!
8
Using Thread Pools package java.util.concurrent; public class ThreadPoolExecutor implements ExecutorService { public ThreadPoolExecutor( int corePoolSize, int maximumPoolSize long keepAliveTime, TimeUnit unit, BlockingQueue workQueue ThreadFactory threadFactory); //optional RejectedExecutionHandler handler; optional } // plus additional methods… This interface controls how pools are managed If corePoolSize == maximumPoolSize constant number of threads If corePoolSize < maximumPoolSize threads can be created/destroyed Do read the textbooks’s java supplement on threads & see javadocs for more. Introducing: The ThreadPoolExecutor class!
9
Using Thread Pools package java.util.concurrent; public class ThreadPoolExecutor implements ExecutorService { public ThreadPoolExecutor( int corePoolSize, int maximumPoolSize long keepAliveTime, TimeUnit unit, BlockingQueue workQueue ThreadFactory threadFactory); //optional RejectedExecutionHandler handler; optional } // plus additional methods… This interface controls how pools are managed If corePoolSize == maximumPoolSize constant number of threads If corePoolSize < maximumPoolSize threads can be created/destroyed ThreadFactory creates new threads on command Do read the textbooks’s java supplement on threads & see javadocs for more. Introducing: The ThreadPoolExecutor class!
10
Using Thread Pools package java.util.concurrent.Executors; public class Executors extends Object { static Callable Callable( Runnable task); // returns a callable object that can run given task static ThreadFactory defaultThreadFactory(); // returns a ThreadFactory used to create threads static ExecutorService newFixedThreadPool(int numThreads); // Creates a thread pool that reuses a fixed number //of threads operating off a shared unbounded queue. static ExecutorService newSingleThreadExecutor(); // as you would guess... Creates just a single thread operating with // a shared unbounded queue } // plus additional methods… This interface is a bit (!) simpler than the ThreadPoolExecutor class (previous chart) Introducing: The Executors class! (it’s static and simpler)
11
Let’s put it all together…
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.