Java Concurrency in practice Chapter 9 GUI Applications

Similar documents
[module lab 2.2] GUI FRAMEWORKS & CONCURRENCY

Threads Questions Important Questions

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

CS 160: Interactive Programming

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

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

Interface Implementation

Last Time. Think carefully about whether you use a heap Look carefully for stack overflow Especially when you have multiple threads

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 12 09/29/2016

Event Dispatch. Dispatching events to windows and widgets.

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 09/17/2015

Computer-System Architecture (cont.) Symmetrically Constructed Clusters (cont.) Advantages: 1. Greater computational power by running applications

CST242 Concurrency Page 1

Multithreading and Interactive Programs

Introduction to concurrency and GUIs

(Incomplete) History of GUIs

Learning from Bad Examples. CSCI 5828: Foundations of Software Engineering Lecture 25 11/18/2014

All the Swing components start with J. The hierarchy diagram is shown below. JComponent is the base class.

THE CPU SPENDS ALMOST ALL of its time fetching instructions from memory

2. The system of... generally ran one job at a time. These were called single stream batch processing.

Operating System Services

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

Concurrent Programming Synchronisation. CISTER Summer Internship 2017

Taming Dava Threads. Apress ALLEN HOLUB. HLuHB Darmstadt

Java Thread Programming By Paul Hyde

Lecture 2: September 9

Control Abstraction. Hwansoo Han

CS5412: TRANSACTIONS (I)

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

Operating Systems (234123) Spring (Homework 3 Wet) Homework 3 Wet

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

CSE 4/521 Introduction to Operating Systems. Lecture 29 Windows 7 (History, Design Principles, System Components, Programmer Interface) Summer 2018

Questions answered in this lecture: CS 537 Lecture 19 Threads and Cooperation. What s in a process? Organizing a Process

Event Dispatch. Interactor Tree Lightweight vs. Heavyweight Positional Dispatch Focus Dispatch. Event Architecture. A pipeline: Event Capture

Virtual Machine Design

Operating Systems. Operating Systems

Kotlin/Native concurrency model. nikolay

Database Architectures

CSCI 4717 Computer Architecture

Executive Summary. It is important for a Java Programmer to understand the power and limitations of concurrent programming in Java using threads.

Thread Safety. Review. Today o Confinement o Threadsafe datatypes Required reading. Concurrency Wrapper Collections

Safety SPL/2010 SPL/20 1

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

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

Operating Systems Overview. Chapter 2

THREADS AND CONCURRENCY

Asynchronous Database Access with Qt 4.x

Intro to multi-threaded programming in Java

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

PROVING THINGS ABOUT PROGRAMS

COMP 213. Advanced Object-oriented Programming. Lecture 23. Shared Variables and Synchronization

Operating Systems. Lecture 09: Input/Output Management. Elvis C. Foster

Java Threads. COMP 585 Noteset #2 1

Chapter 2. Operating-System Structures

What s An OS? Cyclic Executive. Interrupts. Advantages Simple implementation Low overhead Very predictable

Practical Concurrency. Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited.

ò Server can crash or be disconnected ò Client can crash or be disconnected ò How to coordinate multiple clients accessing same file?

NFS. Don Porter CSE 506

Threads. Computer Systems. 5/12/2009 cse threads Perkins, DW Johnson and University of Washington 1

Chapter 4: Threads. Operating System Concepts 9 th Edition

15-323/ Spring 2019 Homework 5 Due April 18

Chapter 4: Multi-Threaded Programming

Page 1. Human-computer interaction. Lecture 2: Design & Implementation. Building user interfaces. Users and limitations

CS108, Stanford Handout #22. Thread 3 GUI

OPERATING SYSTEMS. Prescribed Text Book. Operating System Principles, Seventh Edition. Abraham Silberschatz, Peter Baer Galvin and Greg Gagne

Concurrency in Object Oriented Programs 1. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

Real-Time and Concurrent Programming Lecture 4 (F4): Monitors: synchronized, wait and notify

SSC - Concurrency and Multi-threading Java multithreading programming - Synchronisation (II)

Java Enterprise Edition

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

Event Dispatch. Interactor Tree Lightweight vs. Heavyweight Positional Dispatch Focus Dispatch. 2.4 Event Dispatch 1

Transactum Business Process Manager with High-Performance Elastic Scaling. November 2011 Ivan Klianev

Parallel Programming: Background Information

Parallel Programming: Background Information

POSIX Threads: a first step toward parallel programming. George Bosilca

Unit 2 : Computer and Operating System Structure

Persistent Data Structures and Managed References

Multi-threading in Java. Jeff HUANG

CSc33200: Operating Systems, CS-CCNY, Fall 2003 Jinzhong Niu December 10, Review

Database Architectures

MULTIPROCESSORS AND THREAD LEVEL PARALLELISM

CSE 374 Programming Concepts & Tools

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Introduction to Computer Systems and Operating Systems

Java Programming. Price $ (inc GST)

Asynchronous Programming Under the Hood. Week 6

CSE 153 Design of Operating Systems Fall 2018

Chapter 13: I/O Systems. Operating System Concepts 9 th Edition

REAL-TIME MULTITASKING KERNEL FOR IBM-BASED MICROCOMPUTERS

Thread-Local. Lecture 27: Concurrency 3. Dealing with the Rest. Immutable. Whenever possible, don t share resources

Concurrent Programming in C++ Venkat

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

CSE 451 Midterm 1. Name:

Virtual Memory. Chapter 8

CS11 Java. Fall Lecture 7

Chapter 4: Threads. Operating System Concepts 9 th Edition

The New Java Technology Memory Model

CSI3131 Final Exam Review

Fall 2015 COMP Operating Systems. Lab 06

Transcription:

Java Concurrency in practice Chapter 9 GUI Applications INF329 Spring 2007 Presented by Stian and Eirik 1

Chapter 9 GUI Applications GUI applications have their own peculiar threading issues To maintain safety, certain tasks must run in Swing event thread Long-running tasks in event thread =>UI become unresponsive Swing data structures not thread-safe 2

Chapter 9 GUI Applications Nearly all GUI toolkits, single-threaded subsystems All GUI activity confined to one thread Usual some activities run partially in an application thread and partially in event thread Even though the GUI frameworks themselves are single-threaded subsystems, application may not be Need to consider threading issues carefully when writing GUI code 3

Why are GUIs Single-threaded? Old days GUI applications were single-threaded GUI events were processed from "main event loop" Modern GUI frameworks only slightly different Create a dedicated event dispatch thread for handling GUI events 4

Why are GUIs Single-threaded? Have been many attempts to write multithreaded GUI frameworks Problems with race conditions Deadlocks Actions initiated by user tend to "bubble up" from OS A mouse click detected by OS Turned into "mouse click" event by toolkit Delivered to application listener as higher level event such as a "button pressed" event 5

Why are GUIs Single-threaded? Almost all GUI frameworks eventually arrived at the single-threaded event queue model Dedicated thread fetches events off a queue Dispatches fetched events to applicationdefined event handlers 6

Why are GUIs Single-threaded? Factoring user interactions into cooperating model, view, and controller objects greatly simplifies implementing GUI applications MVC pattern leads to deadlock in multithreaded GUI applications Raises risk of inconsistent lock ordering E.g. Controller calls model Model notifies view - something has changed Controller might also call view View may call back into model to query the model state => inconsistent lock ordering => attendant risk of deadlock 7

Why are GUIs Single-threaded? Single-threaded GUI frameworks achieve thread safety via thread confinement All GUI objects accessed exclusively from event thread Pushes some of the thread safety burden back onto the application developer Developer must make sure these objects are properly confined 8

Sequential Event Processing GUI applications are oriented around processing fine-grained events such as mouse clicks, key presses, or timer expirations Events are a kind of task The event handling machinery provided by AWT and Swing is structurally similar to an Executor 9

Sequential Event Processing Single thread for processing GUI tasks Tasks processed sequentially One task finishes before the next begins No two tasks overlap No need to worry about interference from other tasks 10

Sequential Event Processing If a task takes a long time to execute, other tasks must wait until its done If waiting tasks responsible for responding to user input or visual feedback =>application appear frozen Tasks that execute in the event thread must return control to the event thread quickly Long-running task must run in another thread To update a progress indicator while a long-running task executes or provide visual feedback when it completes, code in event thread must be executed Quickly getting complicated 11

Thread confinement in Swing All Swing components and data model objects are confined to event thread => any code that accesses these objects must run in event thread GUI objects are kept consistent by thread confinement,not by synchronization Tasks that run in the event thread don t need to worry about synchronization when accessing presentation objects Presentation objects cannot be accessed from outside the event thread at all 12

Thread confinement in Swing The Swing single-thread rule: Swing components and models should be created, modified, and queried only from the event-dispatching thread.. Exist a few exceptions See SwingUtilities 13

Short-running GUI tasks For simple, short-running tasks, the entire action can stay in the event thread For longer-running tasks, some of the processing should be offloaded to another thread Example on short running task: a button whose color changes randomly when pressed 14

Short-running GUI tasks Rule: As long as tasks are short-lived and access only GUI objects (or other threadconfined or thread-safe application objects), you can almost totally ignore threading concerns and do everything from the event thread, and the right thing happens.. 15

Short-running GUI tasks - MVC Swing splits most visual components into two objects - model and view Data to be displayed resides in the model Rules governing how it is displayed reside in the view Model objects can fire events indicating that model data has changed Views subscribe to the events View receives event indicating model data may have changed Queries model for new data Updates display Control never leaves event thread 16

Long-running GUI tasks If all tasks were short-running Entire application could run within event thread No need to pay any attention to threads at all Normally, GUI applications may execute tasks that may take longer than the user is willing to wait spell checking background compilation fetching remote resources etc 17

Long-running GUI tasks Rule: Long-running tasks must run in another thread so the GUI remains responsive to user while they run.. In Swing: Easy to have a task run in event thread Doesn't provide any mechanism for helping GUI tasks execute code in other threads Solution: Create own Executor for processing long-running tasks e.g. a cached thread pool 18

Long-running GUI tasks Getting long-running task out of event thread in a "fire and forget" manner Usually visual feedback when a longrunning task completes Cannot access presentation objects from background thread 19

Long-running GUI tasks Completion the task must submit another task to run in the event thread to update the user interface Example: Action listener first dims the button Sets a label indicating that a computation is in progress Submits a task to the background executor When that task finishes, it queues another task to run in the event thread Reenables the button Restores the label text 20

Long-running GUI tasks 21

Cancellation Rule: Any task that takes long enough to run in another thread probably also takes long enough that the user might want to cancel it.. Could implement cancellation directly using thread interruption Easier to use Future Designed to manage cancellable tasks 22

Cancellation 23

Cancellation RunningTask is confined to event thread No synchronization is required when setting or checking it StartButton listener ensures only one background task running at a time Better to be notified when task completes E.g. cancel button could be disabled 24

Progress and completion indication Using a Future to represent long-running task simplifies implementing cancellation FutureTask also has a done hook that similarly facilitates completion notification When background Callable completes => done called Done trigger a completion task in event thread Construct a BackgroundTask class providing an oncompletion hook thats called in event thread See example class BackgroundTask 25

SwingWorker Techniques showed in FutureTask and Executor examples can be applied to any single-threaded GUI framework In Swing, many of the features developed in the examples are provided by the SwingWorker class Various versions of SwingWorker have been published in The Swing Connection and The Java Tutorial Updated version is included in Java 6 26

Shared data models Swing presentation objects, including data model objects, are confined to the event thread In simple GUI programs All mutable state held in presentation objects Only thread beside event thread is main thread Enforcing single-thread rule easy: don't access data model or presentation components from main thread More complicated programs May use other threads to move data to or from a persistent store, e.g. file system or database 27

Shared data models In simplest case, data in the data model: Entered by the user Loaded statically from file / other data source at application startup Never touched by any thread other than event thread Sometimes presentation model object is only a view onto another data source, e.g. database, file system, or remote service. More than one thread is likely to touch the data as it goes into or out of the application. 28

Shared data models E.g. display content of remote file system using a tree control Enumerate entire file system before displaying tree control Takes much time and memory Enumerating a single directory on a remote volume can take long time Tree can be lazily populated as nodes are expanded Enumeration in a background task When background task completes Push data from background task to event thread by posting a task with invokelater Have event thread poll to see if the data is available 29

Shared data models Initiating a long-running, cancellable task with BackgroundTask 30

Thread-safe data models Responsiveness not unduly affected by blocking Problem of multiple threads operating on data can be addressed with a thread-safe data model If data model supports fine-grained concurrency, event thread and background threads should be able to share it Thread-safe data models must generate events when the model has been updated => Views can be updated when the data changes 31

Thread-safe data models Sometimes possible to get thread safety, consistency and good responsiveness with a versioned data model as CopyOnWriteArrayList When acquirering iterator for a CopyOnWrite collection, iterator traverses collection as it existed when the iterator was created CopyOnWrite collections offer good performance only when traversals greatly outnumber modifications Building versioned data structures that provide efficient concurrent access and not retain old versions of data longer than needed is not easy32

Split data models Swing table model classes like TableModel and TreeModel are the official repository for data to be displayed Model objects are often themselves "views" of other objects managed by the application A program that has both a presentation-domain and an applicationdomain data model is said to have a split-model design 33

Split data models Presentation model is confined to the event thread and the shared model Shared model thread-safe May be accessed by event thread and application threads Presentation model registers listeners with shared model Can be notified of updates Presentation model can be updated from shared model Retrieve data directly from shared model 34

Other Forms of Single-threaded Subsystems Thread confinement not restricted to GUI`s Can be used whenever a facility is implemented as a single-threaded subsystem Sometimes thread confinement is forced on the developer for reasons that have nothing to do with avoiding synchronization or deadlock Some native libraries require all access to the library, even loading the library with System.loadLibrary, is made from the same thread 35

Summary GUI frameworks are nearly always implemented as single-threaded subsystems All presentation-related code runs as tasks in an event thread. Long-running tasks can compromise responsiveness Should be executed in background threads Helper classes can simplify development of long-running tasks that have both GUI and non- GUI components 36