…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.

Slides:



Advertisements
Similar presentations
Practical Session 6 Multitasking vs. multithreading Threads Concurrency vs. Parallelism Java Threads Thread confinement Object/Class Immutability.
Advertisements

Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
50.003: Elements of Software Construction Week 10 Thread Pool.
CSC Multiprocessor Programming, Spring, 2011 Outline for Chapter 6 – Task Execution Dr. Dale E. Parson, week 7.
Multithreaded Programs in Java. Tasks and Threads A task is an abstraction of a series of steps – Might be done in a separate thread – Java libraries.
Concurrency 101 Shared state. Part 1: General Concepts 2.
CP — Concurrent Programming 9. Condition Objects Prof. O. Nierstrasz Wintersemester 2005 / 2006.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
7. Liveness and Asynchrony Prof. O. Nierstrasz. Roadmap  Asynchronous invocations  Simple Relays —Direct invocations —Thread-based messages —Command-based.
Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates.
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
Chapter 5: Threads Overview. Multithreading Models. Threading Issues. Thread Pools. Java ThreadLocal. Pthreads. Solaris 2 Threads. Java Threads.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
1 Thread Pools Representation and Management of Data on the Internet.
 2007 Pearson Education, Inc. All rights reserved. 1 Ch23 Multithreading: OBJECTIVES In this chapter you will learn:  What threads are and why they are.
Lecture 10 – Using Thread Pools Housekeeping:  exam return Monday  Programming assignment on Monday Purpose: practice use of the mechanisms we’ve discussed.
Java 5 Threading CSE301 University of Sunderland Harry Erwin, PhD.
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).
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L20 (Chapter 24) Multithreading.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Concurrency with Java Varadha Sundaram. Programming Paradigms Single Process Multi Process Multi Core/Multi Thread.
Ant Colony Optimization A threaded application in Java.
PROG Mobile Java Application Development PROG Mobile Java Application Development Event Handling Creating Menus.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Scheduling and Tasks Spring scheduling.
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.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
CSC 212 Stacks & Queues. Announcement Many programs not compiled before submission  Several could not be compiled  Several others not tested with javadoc.
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.
1 Threads  Sequential Execution: Here statements are executed one after the other.They consider only a single thread of execution, where thread is an.
Java Threads 1 1 Threading and Concurrent Programming in Java Queues D.W. Denbo.
Concurrent Programming and Threads Threads Blocking a User Interface.
Synchronizing threads, thread pools, etc.
Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass.
Computer Science 313 – Advanced Programming Topics.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
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.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Multithreading [Modified]
1 G53SRP: Clocks and Time in Java Chris Greenhalgh School of Computer Science.
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multi-Threading in Java
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
CSC Multiprocessor Programming, Spring, 2012 Chapter 8 – Applying Thread Pools Dr. Dale E. Parson, week 10.
Java Server Sockets ServerSocket : Object to listen for client connection requests Throws IOException accept() method to take the client connection. Returns.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Advanced Tools for Multi- Threads Programming Avshalom Elmalech Eliahu Khalastchi 2010.
Thread Pools CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
1 Stacks Abstract Data Types (ADTs) Stacks Application to the analysis of a time series Java implementation of a stack Interfaces and exceptions.
Java Thread Programming
Principles of Software Development
A brief intro to: Parallelism, Threads, and Concurrency
Multithreading / Concurrency
Thread Pools (Worker Queues) cs
Thread Pools (Worker Queues) cs
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
Threads Chate Patanothai.
Multithreaded Programming
Threads in Java James Brucker.
Using threads for long running tasks.
some important concepts
Presentation transcript:

…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 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!

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

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

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!

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!

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!

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!

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!

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)

Let’s put it all together…