1 Thread Pools Representation and Management of Data on the Internet.

Slides:



Advertisements
Similar presentations
Operating Systems: Monitors 1 Monitors (C.A.R. Hoare) higher level construct than semaphores a package of grouped procedures, variables and data i.e. object.
Advertisements

Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
50.003: Elements of Software Construction Week 10 Thread Pool.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Practice Session 7 Synchronization Liveness Guarded Methods Model
CPU Scheduling CS 3100 CPU Scheduling1. Objectives To introduce CPU scheduling, which is the basis for multiprogrammed operating systems To describe various.
Designing a thread-safe class  Store all states in public static fields  Verifying thread safety is hard  Modifications to the program hard  Design.
Software Engineering Oct-01 #11: All About Threads Phil Gross.
Synchronization in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
1 Thread Pools. 2 What’s A Thread Pool? A programming technique which we will use. A collection of threads that are created once (e.g. when server starts).
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
© Amir Kirsh Threads Written by Amir Kirsh. 2 Lesson’s Objectives By the end of this lesson you will: Be familiar with the Java threads syntax and API.
Threads Written by Amir Kirsh, Dr. Yaron Kanza. Edited by Liron Blecher.
Multithreading.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
- Tausief Shaikh (Senior Server developer). Introduction Covers sense of responsibility towards Project development in IT Focusing on memory and CPU utilizations.
Threads some important concepts Simon Lynch
Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Semaphores, Locks and Monitors By Samah Ibrahim And Dena Missak.
1 (Worker Queues) cs 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.
Threading Eriq Muhammad Adams J
Synchronizing threads, thread pools, etc.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Spring/2002 Distributed Software Engineering C:\unocourses\4350\slides\DefiningThreads 1 Reusing threads.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Consider the program fragment below left. Assume that the program containing this fragment executes t1() and t2() on separate threads running on separate.
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
1 Software Construction and Evolution - CSSE 375 Exception Handling – Chaining & Threading Steve Chenoweth Office: Moench Room F220 Phone: (812)
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Comunication&Synchronization threads 1 Programación Concurrente Benemérita Universidad Autónoma de Puebla Facultad de Ciencias de la Computación Comunicación.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
CSC CSC 143 Threads. CSC Introducing Threads  A thread is a flow of control within a program  A piece of code that runs on its own. The.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
CPU Scheduling Operating Systems CS 550. Last Time Deadlock Detection and Recovery Methods to handle deadlock – Ignore it! – Detect and Recover – Avoidance.
…in java DThread Pools x. Thread Pools: Why? Source code updates – copy/paste of run/runnable method for each thread is exhausting There is overhead to.
Producer/Consumer CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Java Thread Programming
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreading / Concurrency
Thread Pools (Worker Queues) cs
CPS 310 midterm exam #1, 2/19/2016 Your name please: ___________________ NetID:___________ /40 /40 /50.
Multi Threading.
Thread Pools (Worker Queues) cs
Practice Session 8 Lockfree LinkedList Blocking queues
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Threads and Concurrency in Java: Part 2
Threads II IS
Distributed Systems - Comp 655
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
Multithreading.
Threads in Java James Brucker.
Software Engineering and Architecture
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
some important concepts
Presentation transcript:

1 Thread Pools Representation and Management of Data on the Internet

2 Why Thread Pools? Thread pools help achieve optimum resource utilization Common in server applications where –The processing of each individual task is short- lived and the number of requests is large –The overhead of creating a new thread for each request is significant –Servers should not spend more time and consume more system resources creating and destroying threads than processing actual user requests –Creating too many threads in one JVM can cause the system to run out of memory

3 Reusing Threads By reusing threads for multiple tasks: –The thread-creation overhead is spread over many tasks –The delay introduced by thread creation is eliminated

4 A Wrong Implementation Consider the case where –There is a pool of threads –Each 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?

5 Work Queue Worker Threads wait() Q is Empty All the worker threads wait for tasks A Possible Solution

6 Work Queue Worker Threads Task The number of worker threads is fixed. When a task is inserted to the queue, notify is called.

7 Work Queue Worker Threads notify() The number of worker threads is fixed. When a task is inserted to the queue, notify is called.

8 Work Queue Worker Threads The task is executed by the tread

9 Work Queue Worker Threads The task is executed by the tread Task notify()

10 Work Queue Worker Threads The remaining tasks are executed by the tread

11 Work Queue Worker Threads When a task ends the tread 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)

12 Work Queue Worker Threads A new task is executed by the released tread

13 public class WorkQueue { private final int nThreads; private final PoolWorker[] threads; private final LinkedList queue; public WorkQueue(int nThreads) { this.nThreads = nThreads; queue = new LinkedList(); threads = new PoolWorker[nThreads]; for (int i=0; i<nThreads; i++) { threads[i] = new PoolWorker(); threads[i].start(); }

14 public void execute(Runnable r) { synchronized(queue) { queue.addLast(r); queue.notify(); } Why is synchronized needed? What does the notify wake?

15 private class PoolWorker extends Thread { public void run() { Runnable r; while (true) { synchronized(queue) { while (queue.isEmpty()) { try { queue.wait(); } catch (InterruptedException ignored) { } } r = (Runnable)queue.removeFirst(); } Why is synchronized needed?

16 // If we don't catch RuntimeException, // the pool could leak threads try { r.run(); } catch (RuntimeException e) { // You might want to log something here } Why is run called and not start? What happens if we don’t catch the Runtime Exception?

17 Risks in Using Thread Pools A deadlock that is caused when –there are no free threads, –the tasks that are running wait for some task T, and –T waits for a free thread (since the number of running threads is limited) Threads consume memory and if they are active the context switch consumes time (can significantly slowdown the system performance) May cause starvation of threads for other tasks

18 Risks in Using Thread Pools Incorrect implementation may cause –Threads that wait and never being notified –Thread leakage – threads that are taken from the pool and never being returned –Tasks that are stalled for a very long time (e.g., waiting for a user response where the user already gone home …) –Too many requests that arrive at the same time and consume too much resources

19 Some Solutions Do not queue tasks that wait synchronously for results from other tasks If the program must wait for a resource, such as an I/O completion, specify a maximum wait time, and then fail or re-queue the task for execution at a later time Tune the thread pool size according to the number and characterizations of expected tasks