Synchronizing threads, thread pools, etc.

Slides:



Advertisements
Similar presentations
Practice Session 7 Synchronization Liveness Deadlock Starvation Livelock Guarded Methods Model Thread Timing Busy Wait Sleep and Check Wait and Notify.
Advertisements

Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
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.
Multi-threaded applications SE SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions.
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.
CS 11 java track: lecture 7 This week: Web tutorial:
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
1 L49 Multithreading (1). 2 OBJECTIVES  What threads are and why they are useful.  How threads enable you to manage concurrent activities.  The life.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
28-Jun-15 Producer-Consumer An example of using Threads.
Definitions Process – An executing program
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
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.
Multithreading.
1 CSCI 6900: Design, Implementation, and Verification of Concurrent Software Eileen Kraemer August 19 th, 2010 The University of Georgia.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
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.
1 Concurrent Languages – Part 1 COMP 640 Programming Languages.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
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.
Synchronized and Monitors. synchronized is a Java keyword to denote a block of code which must be executed atomically (uninterrupted). It can be applied.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Multithreading Chapter Introduction Consider ability of human body to ___________ –Breathing, heartbeat, chew gum, walk … In many situations we.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Multithreading [Modified]
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
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.
Monitors CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
1 G53SRP: Java Concurrency Control (2) – wait/notify Chris Greenhalgh School of Computer Science.
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.
Advanced Tools for Multi- Threads Programming Avshalom Elmalech Eliahu Khalastchi 2010.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Java.util.concurrent package. concurrency utilities packages provide a powerful, extensible framework of high-performance threading utilities such as.
SCJP 9/10 Threads.
Principles of Software Development
A brief intro to: Parallelism, Threads, and Concurrency
Multithreading / Concurrency
Thread Pools (Worker Queues) cs
Multi Threading.
Thread Pools (Worker Queues) cs
Multithreaded Programming in Java
Critical sections, locking, monitors, etc.
23 Multithreading.
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreading Chapter 23.
Multithreading.
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
9. Threads SE2811 Software Component Design
Computer Science 2 06A-Java Multithreading
NETWORK PROGRAMMING CNET 441
9. Threads SE2811 Software Component Design
Software Engineering and Architecture
CMSC 202 Threads.
some important concepts
9. Threads SE2811 Software Component Design
Presentation transcript:

Synchronizing threads, thread pools, etc. Concurrency Synchronizing threads, thread pools, etc. Threads in Java

Creating threads Class A extends Thread { public void run() { … } A a = new A(); a.start(); A may not extend other classes. Class B implements Runnable { public void run() { … } B b = new B(); Tread t = new Thread(b); t.start(); B may extend another class. Decouples task submission from thread scheduling, etc. Threads in Java

Synchronizing threads Having threads in a programming language is a nice feature, but threads have to be controlled. If you have 2 or more threads with reference to the same object, the threads might execute methods on that object simultaneously. This must be controlled Threads in Java

Race conditions and critical sections 2 or more threads are reading or writing share data – and the final result depends on the timing of the thread scheduling. Race conditions are generally a bad thing! Critical section Part of a program (whole method or just a part of a method) where race conditions might happen. To avoid race conditions We want to make sure that at most one thread executes the critical section at any point in time. Threads must be synchronized. Threads in Java

Locks on objects Every object has an associated lock. At most one thread can have the lock at any point in time. A thread acquire the lock of the object when it enters a synchronized block (i.e. critical section) If another thread holds the lock on the object the entering thread has to wait (hopefully not forever). A thread releases the lock when it leaves the synchronized block. Threads in Java

Synchronization: Java syntax Public synchronized method() { // critical section } Often the whole method is a critical section. You synchronize on the current object, i.e. this. Public method() { … synchronized(obj) { // critical section } Sometimes only part of a method is a critical section. You synchronize on the object mentioned. Less synchronization, means more concurrency. Threads in Java

Reentrant locks If a thread has a the lock on some object, and then calls another synchronized method, the thread does not have to wait for itself to release the lock The lock is not released when it leaves the latter method Since the thread had the lock before entering the method. Threads in Java

wait Entering a synchronized method a thread might realize that it is not in a state to fulfill its task – and it cannot simply return from the method because the return value is not “ready”. The thread must wait Call wait() Method from class Object wait() really means this.wait() Releases the lock on the object Another thread can run, hopefully “fixing the state” Waits for another thread to call a notify() / notifyAll() Threads in Java

notify() and notifyAll() Obj.notify() Wakes up a single thread waiting on obj. The thread is randomly chosen Obj.notifyAll() Wakes up all threads waiting on obj. Generally you want to use notifyAll() not notify() Threads in Java

Wait “Code pattern” synchronized (obj) { while (conditionDoesNotHold) { obj.wait(); } // perform action appropriate to the condition http://java.sun.com/books/tutorial/essential/threads/waitAndNotify.html wait() should always be called in a loop, since obj.notifyAll() wakes up all thread waiting for the obj. Only the first (quickest) should execute the synchronized block. Other thread go back to wait. Threads in Java

3 versions of wait wait() wait(long timeout) Waits indefinitely for notification hopefully not forever wait(long timeout) Waits for notification or until timeout miliseconds has elapsed wait(long timeout, int nanos) Waits for notification or until timeout miliseconds + nanos nanoseconds have elapsed. Threads in Java

Collection framework Vector, HashTable Synchronized wrappers Old collections From before the collections framework Methods are synchonized Synchronized wrappers Modern collections From the collections framework Methods are not synchronized Methods can be synchronized using the synchronized wrappers Static methods in class Collections Colletion synchronizedCollection(Collection c) List synchronizedList(List l) Set synchronizedSet(Set s) Map synchronizedMap(Map m) Threads in Java

Thread pools Creating a new thread object takes relatively much time Idea: Recycle thread objects. Keep threads in a thread pool. Request thread from pool. Put used thread back in the pool. Java.util.concurrent offers more implementations of this idea New in Java 5.0 Example http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorService.html Threads in Java

Thread pool implementations The class Executors has 2 static methods to create thread pools ExecutorService newFixedThreadPool(int nThreads) Pool of a fixed size ExecutorService newCachedThreadPool() Creates new threads as needed. New threads are added to the pool, and recycled. ExecutorService has an execute method void execute(Runnable command) Threads in Java

Callable vs. runnable interface Callable has 1 method V call() throws Exception Can return a value Can throw exceptions Can be executed using an Executor object Runnable has 1 method Void run() No return value No exceptions Can be executed using an Executor object Threads in Java

Threads in Java Swing Java Swing has a single main thread Event-handling Painting visual components Called the event-dispatching-thread (EDT) Your threads should be executed by the EDT To avoid deadlock Threads in Java

Methods in javax.swing.Utilities The class javax.swing.Utilities has 2 interesting static methods SwingUtilities.invokeLater(Runnable r) Returns as soon as r is handed over to the EDT. Asynchronous Used for updating visual components. Java 1.3 Calls java.awt.EventQueue.invokeLater(Runnable r) SwingUtilities.invokeAndWait(Runnable r) Returns when r has finished running Synchronous Threads in Java

Starting a Swing based application class MyFrame extends JFrame { … } public static void main(String [] args) { Runnable r = new Runnable() { public void run() { new MyFrame().setVisible(); EventQueue.invokeLater(r); This is (almost) the code NetBeans 4.1 generates when you make a new JFrame class! Threads in Java

Use threads to improve performance Move time-consuming tasks out of the main thread GUI responds faster Create special threads for the time-consuming tasks. Examples: Reading data from files, network connections, databases, etc. Long running computations Threads in Java

References Sun Microsystems The Java Tutorial, Threads http://java.sun.com/docs/books/tutorial/essential/threads/index.html Sun Microsystems Java 2 Platform Standard Edition 5.0 API Specification: java.util.concurrent http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/package-summary.html Doug Lea Concurrent Programming in Java 2nd edition, Addison Wesley 2000 Probably the most important book on concurrent programming in Java – by the author who wrote java.util.concurrent Threads in Java