Thread Pools CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Slides:



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

Graphics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
50.003: Elements of Software Construction Week 10 Thread Pool.
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
1 Thread Pools Representation and Management of Data on the Internet.
1 CS2200 Software Development Lecture: Multi-threading II A. O’Riordan, 2009.
Multithreading A thread is the flow of execution, from beginning to end, of a task in a program. With Java, you can launch multiple threads from a program.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L20 (Chapter 24) Multithreading.
Web Services CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Garbage Collection CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
Multithreading.
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Inheritance CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Java Environment CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Generics CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Tables CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
CORBA – Command Line CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
JFrame and JPanel CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multi-Threaded Programming Design CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
RMI – Command Line CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Trees CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Conditions CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Synchronizing threads, thread pools, etc.
Parallel Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CORBA – Eclipse CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 24 th, 2010 The University of Georgia.
Option Panes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
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.
Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
System Programming Practical Session 4: Concurrency / Safety.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Event-Driven Programming CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Semaphores CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Singleton Pattern Presented By:- Navaneet Kumar ise
CSCI 115 Computer Programming Overview. Computer Software System Software –Operating systems –Utility programs –Language compilers Application Software.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse.
Classes CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Networking Code CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Menus and Toolbars CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Advanced Tools for Multi- Threads Programming Avshalom Elmalech Eliahu Khalastchi 2010.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
…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.
Databases and SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Reading Parameters CSCI 201 Principles of Software Development Jeffrey Miller, Ph.D.
Principles of Software Development
Principles of Software Development
Thread Pools (Worker Queues) cs
Multithreading Lec 23.
Thread Pools (Worker Queues) cs
Section 5.7 Concurrency, Interference, and Synchronization.
Multithreaded Programming in Java
Multithreading Chapter 23.
Principles of Software Development
Principles of Software Development
class PrintOnetoTen { public static void main(String args[]) {
Java Thread.
Threads in Java James Brucker.
Multithreading in java.
CMSC 202 Threads.
Presentation transcript:

Thread Pools CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L

Outline USC CSCI 201L2/6 ▪T▪Thread Pools

Thread Pool Overview ▪If you need to create a lot of threads, it may not be the most efficient to create them by instantiating them all as threads and starting them ›There may be performance issues, such as limiting throughput, if there are too many threads ▪A thread pool helps to manage the number of threads executing concurrently ›If a thread in a thread pool completes execution, it can be reused instead of needing to create another thread ›If a thread terminates due to failing, another thread will be created to replace it ▪The execute method will execute the thread at some time in the future, determined by the Executor USC CSCI 201L3/6 Thread Pools

Thread Management ▪Thread pools also give us more information about the state of threads ▪We are able to find out if all of the threads that were invoked have completed ›ExecutorService.isTerminated() ▪We can also make sure no additional threads can be created by terminating the pool (though existing threads will still be able to complete) ›ExecutorService.shutdown() ›ExecutorService.shutdownNow() will kill all currently executing threads in the pool USC CSCI 201L4/6 Thread Pools

Executors Class ▪The Executors class provides factory and utility methods for executing threads ▪newCachedThreadPool() ›Creates a thread pool that creates new threads as needed and reuses previously constructed threads when they are available ▪newFixedThreadPool(int numThreads) ›Creates a thread pool that reuses a fixed number of threads. At any point, a maximum of numThreads will be executing ▪newScheduledThreadPool(int corePoolSize) ›Creates a thread pool that can schedule commands to run after a given delay or to execute periodically ▪newSingleThreadExecutor() ›Creates an Executor that uses a single worker thread USC CSCI 201L5/6 Thread Pools

Thread Pool Example 1 import java.util.concurrent.ExecutorService; 2 import java.util.concurrent.Executors; 3 4 public class Test { 5 public static void main(String[] args) { 6 System.out.println("First line"); 7 ExecutorService executor = Executors.newFixedThreadPool(3); 8 executor.execute(new TestThread('a')); 9 executor.execute(new TestThread('b')); 10 executor.execute(new TestThread('c')); 11 executor.shutdown(); // threads will still complete, executor.shutdownNow() otherwise 12 System.out.println("Last line"); 13 } 14 } class TestThread extends Thread { 17 private char c; 18 public TestThread(char c) { 19 this.c = c; 20 } 21 public void run() { 22 for (int i=0; i < 20; i++) { 23 System.out.print(i + "" + c + " "); 24 } 25 System.out.println(""); 26 } 27 } USC CSCI 201L6/6 Thread Pools