1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once...

25
1 (Worker Queues) cs236607

Transcript of 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once...

Page 1: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

1

(Worker Queues)

cs236607

Page 2: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

2

What is a Thread Pool?A collection of threads that are created once

(e.g. when a server starts)That is, no need to create a new thread for

every client requestInstead, the server uses an already prepared

thread if there is a free one, or waits until there is a free thread

cs236607

Page 3: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

cs236607 3

Why Do We Need a Thread Pool?

Server

Why not to span a thread upon each request?

thread thread thread

To which cases such an architecture is suitable?

Page 4: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

4

Why Using Thread Pools?Thread pools improve resource utilization

The overhead of creating a new thread is significant

Thread pools enable applications to control and bound their thread usageCreating too many threads in one JVM can cause

the system to run out of memory and even crashThere is a need to limit the usage of system

resources such as connections to a database

cs236607

Page 5: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

5

Thread Pools in ServersThread pools are especially important in

client-server applicationsThe processing of each individual task is short-

lived and the number of requests is largeServers should not spend more time and

consume more system resources creating and destroying threads than processing actual user requests

When too many requests arrive, thread pools enable the server to force clients to wait until threads are available

cs236607

Page 6: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

6

The “Obvious” ImplementationThere is a pool of threadsEach task asks for a thread when starting

and returns the thread to the pool after finishing

When there are no available threads in the pool the thread that initiates the task waits till the pool is not empty

What is the problem here? “Synchronized” model - the client waits until the server takes care of its request…

cs236607

Page 7: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

7

The “Obvious” Implementation is ProblematicWhen the pool is empty, the submitting

thread has to wait for a thread to be availableWe usually want to avoid blocking that thread A server may want to perform some actions

when too many requests arriveTechnically, Java threads that finished

running cannot run again

cs236607

Page 8: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

8

Task Queue

Worker Threads

wait()

If Q is Empty

All the worker threads wait for tasks

A Possible SolutionA Possible Solution

Every thread looks for tasks in the queue

cs236607

Page 9: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

9

Task Queue

Worker Threads

Task

The number of worker threads is fixed. When a task is inserted to the queue, notify is called

A Possible SolutionA Possible Solution

“A-synchronized” model: “Launch and forget”

cs236607

Page 10: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

10

Task Queue

Worker Threads

Task

The number of worker threads is fixed. When a task is inserted to the queue, notify is called

A Possible SolutionA Possible Solution

cs236607

notify()

Page 11: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

11

Task Queue

Worker Threads

A Possible SolutionA Possible Solution

cs236607

The task is executed by the thread

Page 12: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

12

Task Queue

Worker Threads

A Possible SolutionA Possible Solution

cs236607

The task is executed by the thread

The remaining tasks are executed by the other threads

Page 13: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

13

Task Queue

Worker Threads

A Possible SolutionA Possible Solution

cs236607

When a task ends, the thread is released

While the Q is not empty, take the task from the Q and run it (if the Q was empty, wait() would have been called)

Page 14: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

14

Task Queue

Worker Threads

A Possible SolutionA Possible Solution

cs236607

A new task is executed by the released thread

Page 15: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

15

Thread Pool Implementation public class TaskManager { LinkedList taskQueue = new LinkedList(); List threads = new LinkedList(); public TaskManager(int numThreads) { for(int i=0; i<numThreads; ++i) { Thread worker = new Worker(taskQueue); threads.add(worker); worker.start(); }} public void execute(Runnable task) { synchronized(taskQueue) { taskQueue.addLast(task); taskQueue.notify(); } } }

cs236607

Page 16: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

16

Thread Pool Implementationpublic class Worker extends Thread { LinkedList taskQueue = null; public Worker(LinkedList queue) { taskQueue = queue; } public void run() { Runnable task = null; while (true) { synchronized (taskQueue) { while (taskQueue.isEmpty()) { try {taskQueue.wait();} catch (InterruptedException ignored) {}} task = (Runnable) taskQueue.removeFirst(); } task.run();}}}

cs236607

Page 17: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

17

Risks in Using Thread PoolsThreads can leak

A thread can endlessly wait for an I/O operation to complete For example, the client may stop the interaction with the socket without closing it properly

What if task.run() throws a runtime exception (as opposed to other exceptions that a programmer of a client application has to catch in order to succeed compiling)?

Solutions: Bound I/O operations by timeouts using

wait(time)Catch possible runtime exceptions

cs236607

Page 18: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

18

Pool SizeWhat is better: to have a large pool or a small

pool?Each thread consumes resources

memory, management overhead, etc.A large pool can cause starvation

Incoming tasks wait for a free threadA small pool can cause starvation

Therefore, you have to tune the thread pool size according to the number and characterizations of expected tasks

There should also be a limit on the size of the task queue (why?)

cs236607

Page 19: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

Handling too Many RequestsWhat is the problem with the server being

overwhelmed with requests?What can a server do to avoid a request

overload?Do not add to the queue all the requests:

ignore or send an error responseUse several pool sizes alternately according to

stress characteristics (but do not change the size too often…)

cs236607 19

Page 20: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

Tuning the Pool SizeThe main goal: Processing should continue

while waiting for slow operations such as I/OWT = estimated average waiting timeST = estimated average processing time for a

request (without the waiting time)About WT/ST+1 threads will keep the

processor fully utilizedFor example, if WT is 20 ms and ST is 5 ms, we

will need 5 threads to keep the processor busy

cs236607 20

Page 21: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

The java.util.concurrent PackageProvides a more advanced mechanisms for

handling concurrency (Since Java 5.0)Includes an implementation of thread pools

cs236607 21

Page 22: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

LockSynchronized sections are like a trap – once

entered, the thread is blocked till …Lock objects provide the ability to check the

availability of the lock and back out, if desiredWhen using Lock objects, lock() and , unlock() are

called explicitly

cs236607 22

Page 23: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

ExecutorThe class Executors has 2 static methods to

create thread poolsExecutorService newFixedThreadPool(int nThreads)

Pool of a fixed sizeExecutorService newCachedThreadPool()

Creates new threads as needed New threads are added to the pool, and recycled

ExecutorService has an execute methodvoid execute(Runnable command)

cs236607 23

Page 24: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

cs236607 24

class NetworkService { private final ServerSocket serverSocket; private final ExecutorService pool; public NetworkService(int port, int poolSize) throws IOException { serverSocket = new ServerSocket(port); pool = Executors.newFixedThreadPool(poolSize); }

public void serve() { try { for (;;) { pool.execute(new Handler(serverSocket.accept())); } } catch (IOException ex) { pool.shutdown(); } } }

Page 25: 1 (Worker Queues) cs236607. 2 What is a Thread Pool? A collection of threads that are created once (e.g. when a server starts) That is, no need to create.

cs236607 25

class Handler implements Runnable { private final Socket socket; Handler(Socket socket) { this.socket = socket; }

public void run() { // read and service request } }