CS11 Java. Fall Lecture 7

Similar documents
Java Threads. Introduction to Java Threads

Need for synchronization: If threads comprise parts of our software systems, then they must communicate.

Java Threads. COMP 585 Noteset #2 1

Introduction to Java Threads

CMSC 330: Organization of Programming Languages

Advanced Concepts of Programming

Threads Questions Important Questions

Overview. CMSC 330: Organization of Programming Languages. Concurrency. Multiprocessors. Processes vs. Threads. Computation Abstractions

Threads and Parallelism in Java

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Principles of Software Construction: Concurrency, Part 2

Java Threads. Written by John Bell for CS 342, Spring 2018

Threads and Java Memory Model

Deadlock and Monitors. CS439: Principles of Computer Systems September 24, 2018

CMSC 330: Organization of Programming Languages

CS 162 Operating Systems and Systems Programming Professor: Anthony D. Joseph Spring 2004

The Dining Philosophers Problem CMSC 330: Organization of Programming Languages

CMSC 433 Programming Language Technologies and Paradigms. Spring 2013

Lecture 9: Introduction to Monitors

CMSC 330: Organization of Programming Languages. The Dining Philosophers Problem

Info 408 Distributed Applications Programming Exercise sheet nb. 4

Deadlock and Monitors. CS439: Principles of Computer Systems February 7, 2018

Overview. Processes vs. Threads. Computation Abstractions. CMSC 433, Fall Michael Hicks 1

CMSC 330: Organization of Programming Languages. Threads Classic Concurrency Problems

Multiple Inheritance. Computer object can be viewed as

Synchronization. Announcements. Concurrent Programs. Race Conditions. Race Conditions 11/9/17. Purpose of this lecture. A8 released today, Due: 11/21

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Threads Chate Patanothai

Multitasking Multitasking allows several activities to occur concurrently on the computer. A distinction is usually made between: Process-based multit

Race Conditions & Synchronization

Synchronization COMPSCI 386

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

CS 2112 Lecture 20 Synchronization 5 April 2012 Lecturer: Andrew Myers

Contribution:javaMultithreading Multithreading Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team

COMP31212: Concurrency A Review of Java Concurrency. Giles Reger

Models of concurrency & synchronization algorithms

27/04/2012. We re going to build Multithreading Application. Objectives. MultiThreading. Multithreading Applications. What are Threads?

More Synchronization; Concurrency in Java. CS 475, Spring 2018 Concurrent & Distributed Systems

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

Synchronization Lecture 23 Fall 2017

CSCD 330 Network Programming

Chapter 32 Multithreading and Parallel Programming

Programming in Parallel COMP755

G52CON: Concepts of Concurrency

Lecture 10: Introduction to Semaphores

Handouts. 1 Handout for today! Recap. Homework #2 feedback. Last Time. What did you think? HW3a: ThreadBank. Today. Small assignment.

Multi-threaded programming in Java

The New Java Technology Memory Model

CS 351 Design of Large Programs Threads and Concurrency

Lecture 29: Java s synchronized statement

Virtual Machine Design

RACE CONDITIONS AND SYNCHRONIZATION

wait with priority An enhanced version of the wait operation accepts an optional priority argument:

UNIT V CONCURRENT PROGRAMMING

Synchronization. CS 475, Spring 2018 Concurrent & Distributed Systems

CMSC 433 Programming Language Technologies and Paradigms. Composing Objects

Synchronization II: EventBarrier, Monitor, and a Semaphore. COMPSCI210 Recitation 4th Mar 2013 Vamsi Thummala

Week 7. Concurrent Programming: Thread Synchronization. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Concurrency in Java Prof. Stephen A. Edwards

Introduction to Locks. Intrinsic Locks

COMPSCI 230 Threading Week8. Figure 1 Thread status diagram [

The University of Texas at Arlington

THREADS AND CONCURRENCY

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

G Programming Languages Spring 2010 Lecture 13. Robert Grimm, New York University

Concurrent Programming using Threads

Computation Abstractions. CMSC 330: Organization of Programming Languages. So, What Is a Thread? Processes vs. Threads. A computer.

What's wrong with Semaphores?

An Introduction to Programming with Java Threads Andrew Whitaker University of Washington 9/13/2006. Thread Creation

Recap. Contents. Reenterancy of synchronized. Explicit Locks: ReentrantLock. Reenterancy of synchronise (ctd) Advanced Thread programming.

Synchronization Lecture 24 Fall 2018

Midterm on next week Tuesday May 4. CS 361 Concurrent programming Drexel University Fall 2004 Lecture 9

Lecture 8: September 30

Synchronization in Java

Synchronization synchronization.

Monitors & Condition Synchronization

Problems with Concurrency. February 19, 2014

Lecture #7: Implementing Mutual Exclusion

Shared-Memory and Multithread Programming

Programming Language Concepts: Lecture 11

Monitors; Software Transactional Memory

COMP346 Winter Tutorial 4 Synchronization Semaphores

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

Only one thread can own a specific monitor

CMSC 132: Object-Oriented Programming II. Threads in Java

Animation Part 2: MoveableShape interface & Multithreading

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Race Conditions & Synchronization

Java Programming Lecture 23

Synchronization in Concurrent Programming. Amit Gupta

Synchronising Threads

Summary Semaphores. Passing the Baton any await statement. Synchronisation code not linked to the data

Multitasking. Multitasking allows several activities to occur concurrently on the computer Levels of multitasking: Process based multitasking

CS 159: Parallel Processing

What is a thread anyway?

Java Monitors. Parallel and Distributed Computing. Department of Computer Science and Engineering (DEI) Instituto Superior Técnico.

Component-Based Software Engineering

Operating Systems. Lecture 4 - Concurrency and Synchronization. Master of Computer Science PUF - Hồ Chí Minh 2016/2017

Contents. 6-1 Copyright (c) N. Afshartous

The University of Texas at Arlington

Transcription:

CS11 Java Fall 2006-2007 Lecture 7

Today s Topics All about Java Threads Some Lab 7 tips

Java Threading Recap A program can use multiple threads to do several things at once A thread can have local (non-shared) resources Threads can share resources, too! Interactions with shared resources must be performed atomically Not doing this produces spurious results Shared resources must be locked carefully, to avoid deadlock and other similar problems

Why Multithreading? Threads impose a (usually small) performance cost Single processor has to switch between several threads, to give each one a time-slice to run in Even with multiple processors, have synchronization costs Sometimes threads perform slow operations e.g. communication over a network Gives other tasks an opportunity to progress, while the slow operation is taking place in a dedicated thread Threads also provide a powerful conceptual model Some programs are simply easier to understand, when implemented with several threads to perform various tasks

This Week s Lab Make last week s web-crawler faster! Lots of time spent sending HTTP request, getting response back Create some number of crawler threads Each will analyze one web page at a time Provides dramatic improvement in performance as long as there aren t too many crawler threads! Need a URL Pool Each crawler thread puts new URLs into the pool Crawlers get next URL to crawl from the pool

The URL Pool URL Pool is a shared resource Crawler threads must interact atomically with it Sometimes, no next URL will be available! How can a thread perform atomic interactions with an object? How can a thread wait for a condition to become true?

Atomic Interactions In Java, every object has a monitor A monitor is a simple mutex ( mutual exclusion ) lock An object can be locked by at most one thread at a time Use synchronized block to lock an object synchronized (sharedobj) {... // Perform atomic operations on shared object. Thread blocks (suspends) until sharedobj s monitor is acquired Thread resumes when sharedobj s monitor is acquired At end of synchronized block, sharedobj s monitor is automatically released

Example: A Thread-Safe FIFO Producer-consumer problem: One thread is producing data Another thread is consuming the data How to interface the two threads? A simple solution: build a thread-safe FIFO First In, First Out queue Both producer and consumer use the FIFO Producer puts data into the FIFO Consumer gets data out of the FIFO Interaction with FIFO must be synchronized!

A Simple FIFO Build a FIFO that uses a LinkedList for storage Give our FIFO a maximum size. If producer is faster than consumer, don t want FIFO to grow out of control! Our FIFO class: public class FIFO { private int maxsize; private LinkedList items; public FIFO(int size) { maxsize = size; items = new LinkedList();...

Putting Items into the FIFO If there is space, add object to end of FIFO and return true. Otherwise, do nothing and return false. FIFO Code: public boolean put(object obj) { boolean added = false; if (items.size() < maxsize) { items.addlast(obj); added = true; return added;

Getting Items from the FIFO If an item is available, remove it and return it If no item is available, return null FIFO Code: public Object get() { Object item = null; if (items.size() > 0) item = items.removefirst(); return item;

FIFO Threading Issues This FIFO code isn t thread-safe! LinkedList isn t thread-safe, so getting and putting at same time can produce spurious results. Bigger issues arise with multiple producers, or multiple consumers. Example: two consumer threads, one item in queue public Object get() { Object item = null; if (items.size() > 0) item = items.removefirst(); return item; Both consumers might see items.size() return 1, then try to remove the item. The FIFO would throw an exception!

Synchronizing FIFO Operations FIFO can use synchronized blocks to ensure thread-safety public Object get() { Object item = null; synchronized (items) { // This thread has exclusive // access to items now. if (items.size() > 0) item = items.removefirst(); return item; Must also make put(object) method thread-safe! Enclose operations on items within a synchronized block

Another FIFO Issue What about when there s nothing to get? Could write a loop that checks regularly ( polls or spins ) // Keep trying until we get something! do { item = myfifo.get(); while (item == null); Polling is very costly! Polling operations almost invariably use way too many CPU resources to be a good idea Always try to find another solution to polling

Passive Waiting Would like threads to wait passively Put a thread to sleep, then wake it up later Accomplished with wait() and notify() methods Defined on java.lang.object (see API docs) Once a thread has synchronized on an object: (i.e. the thread holds that object s monitor) The thread can call wait() on that object to suspend itself The thread releases that object s monitor, then suspends. Can only call wait() on an object if you have actually synchronized on it. If not, IllegalMonitorStateException is thrown!

Wake Up! Another thread can wake up the suspended thread First, the thread must lock the same object as before (It synchronizes on the object.) Then the thread can call notify() or notifyall() to wake up any threads that are suspended on that object. notify() wakes up one thread waiting on that object notifyall() wakes up all threads waiting on that object If no thread is waiting when notify() or notifyall() is called, nothing happens. (It s a no-op.) Can only call notify()/notifyall() on objects that the thread has already locked

Thread Notification When a thread is notified, it immediately tries to relock the object it called wait() on It called wait() inside a synchronized block But the thread that called notify() still holds the lock. When the notifying thread releases the lock, one of the notified threads gets the lock next. The JVM arbitrarily picks one! The notified thread gets to resume execution with exclusive access to the locked object.

How To Use wait() and notify() Common scenario: One thread can t proceed until some condition is true. The thread can call wait() to go to sleep. FIFO: get() method can wait() if no items Another thread changes the state: It knows that the condition is now true! It calls notify() or notifyall() to wake up any suspended threads FIFO: put() method can notify() when it adds something

How to wait() A waiting thread shouldn t assume that the condition is true when it wakes up. If multiple threads are waiting on the same object, and notifyall() is called, another thread may have gotten to the object first. Also, spurious wakeups can occur A thread resumes without being notified (!!!) Can occur depending on how JVM was implemented Can also use wait() with a timeout Wait to be notified, or until this amount of time passes. Always use wait() in a loop that checks the condition

Back to the FIFO Now we can use wait() in our FIFO: public Object get() { Object item = null; synchronized (items) { // This thread has exclusive access to items // Keep waiting until an item is available while (items.size() == 0) Always wait inside of items.wait(); a loop that checks the condition! item = items.removefirst() return item;

Waking the Consumer Now put() must notify waiting consumers public boolean put(object obj) { boolean added = false; synchronized (items) { if (items.size() < maxsize) { items.addlast(obj); added = true; // Added something, so wake a consumer, if any. items.notify(); Call notify() on same object that other threads return added; are waiting on.

One More Issue If producer is faster than consumer, it has no way to wait until there s room in the FIFO! The consumer can passively wait, but Producer has to poll if there s no room in the FIFO This is a simple FIFO. In fact, it s really simple it has other issues too! Example: using a single lock for both gets & puts See java.util.concurrent classes for really sophisticated queues, pools, etc. New in Java 1.5! Written by Doug Lea.

Synchronizing on this An object can synchronize on itself Useful when object manages several shared resources Manually locking multiple resources can lead to deadlock, if you aren t careful FIFO could do this instead of locking items : public Object get() { Object item = null; // Lock my own monitor. synchronized (this) { while (items.size() == 0) wait(); // Call wait() on myself. item = items.removefirst();

Synchronized Methods Synchronizing on this is very common Java provides an alternate syntax: public synchronized Object get() { while (items.size() == 0) wait(); return items.removefirst(); this is locked at beginning of method call this is unlocked at end of method call Can call wait() or notify() inside method Putting synchronized on all methods is an easy way to make a class thread-safe (Don t need to put synchronized on constructors)

Threads and Performance Synchronization incurs a cost Locking and unlocking the mutex takes time Don t use synchronization unless it s necessary Example: java.util.vector, java.util.hashtable Both classes synchronize every single method Don t use them in single-threaded programs Threads should lock shared resources for as little time as possible Keep thread-contention to a minimum

Lab 7 Tips Need a pool of URLDepthPair objects This pool is shared among all web-crawler threads Crawler threads get URLs from pool, add new ones to pool Internals: One LinkedList to keep track of URLs to crawl Another LinkedList for URLs you have seen Methods: Get the next URLDepthPair to process Suspend the thread if nothing is immediately available Add a URLDepthPair to the pool Always add the URL to seen list Only add to pending list if depth is less than max depth If added to pending list, notify any suspended threads

Most Challenging Problem When are we done crawling? How do we know? When all crawler threads are waiting, we re done! (Pending queue had better be empty, too!) URL Pool should keep a count of waiting threads Easy to implement: Increment count immediately before calling wait() Decrement count immediately after wait() returns Main thread can periodically check this count It knows how many crawler threads were requested It needs to print out the results at the end, anyways.

Crawler Threads Create a CrawlerTask that implements Runnable CrawlerTask needs a reference to the URLPool run() method contains a loop: Get a URL from the pool. Download the web page, looking for new URLs. Stick new URLs back into the pool. Go back to the beginning! Process each URL in a helper method (or several helpers) Hint: reuse your code from last week s crawler. Handle exceptions gracefully! If a problem occurs with one URL, go on to the next one!

Web-Crawler Main Method main() drives everything from start to finish Get initial URL, max depth, number of threads from command-line parameters Create a URL pool, add the initial URL to pool Create and start the requested number of threads Could put them into an array, to clean them up later. Not strictly necessary though. Check pool every 0.1 to 1 second for completion When finished, print URLs in the pool s seen list System.exit(0);

Using Threads Create a class that implements Runnable Implement run() method to do your work Pass an instance of your class to Thread constructor CrawlerTask c = new CrawlerTask(pool); Thread t = new Thread(c); Call start() on the new thread object t.start(); The thread will automatically call your object s run() method Thread terminates when run() method ends

Gentle Polling Use Thread.sleep() to pause between checks sleep() is a static method Can throw InterruptedException! (About the nicest way one can poll ) Something like this: while (pool.getwaitcount()!= numthreads) { try { Thread.sleep(100); // 0.1 second catch (InterruptedException ie) { // Ignore!

The Big Picture Lab7 class main() method - Set up pool - Start threads - Monitor pool - Print results - Shut down Threads CrawlerTask CrawlerTask... CrawlerTask URLPool pending seen waitcount

Pool Synchronization URLPool contains several shared resources! Pending list, seen list, count of waiting threads, URLPool object can synchronize on itself. Avoids thread-safety/deadlock issues, etc. URLPool should take care of threading issues internally. Crawler tasks shouldn t have to manually synchronize/wait/notify on pool to use it. Want to encapsulate threading details too!

Java Threading References Concurrent Programming in Java (2 nd ed.) Doug Lea Effective Java Joshua Bloch