CSC 580 - Multiprocessor Programming, Spring, 2011 Chapter 9 – GUI Applications Dr. Dale E. Parson, week 11.

Slides:



Advertisements
Similar presentations
How to Build Multi- threaded Applications in.NET Mazen S. Alzogbi Technology Specialist Microsoft Corporation.
Advertisements

Concurrency (p2) synchronized (this) { doLecture(part2); } synchronized (this) { doLecture(part2); }
CSCC69: Operating Systems
CSC Multiprocessor Programming, Spring, 2011 Outline for Chapter 6 – Task Execution Dr. Dale E. Parson, week 7.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Object Oriented Programming with Java (150704).   Applet  AWT (Abstract Window Toolkit)  Japplet  Swing Types of Applet.
OOP Design Patterns Chapters Design Patterns The main idea behind design patterns is to extract the high level interactions between objects and.
Threads Irfan Khan Myo Thein What Are Threads ? a light, fine, string like length of material made up of two or more fibers or strands of spun cotton,
1 Chapter 7 Graphics and Event Handling. 2 Overview The java.awt and javax.swing packages and their subpackages support graphics and event handling. Many.
 2007 Pearson Education, Inc. All rights reserved. 1 Ch23 Multithreading: OBJECTIVES In this chapter you will learn:  What threads are and why they are.
490dp Synchronous vs. Asynchronous Invocation Robert Grimm.
Silberschatz, Galvin and Gagne  Operating System Concepts Chapter 5: Threads Overview Multithreading Models Threading Issues Pthreads Solaris.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 3: Processes.
Threads and GUIs Confession: I’ve never liked GUI programming very much – so I haven’t done a lot of it  I don’t like the visual design aspects  But.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L20 (Chapter 24) Multithreading.
Process Management. Processes Process Concept Process Scheduling Operations on Processes Interprocess Communication Examples of IPC Systems Communication.
1 Computer System Overview Chapter 1. 2 n An Operating System makes the computing power available to users by controlling the hardware n Let us review.
Chapter 6 Operating System Support. This chapter describes how middleware is supported by the operating system facilities at the nodes of a distributed.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
(c) University of Washington08-1 CSC 143 Models and Views Reading: Ch. 18.
BI-SW Training day Outline What is the EDT? Why should I care about it? How do I treat GUI objects properly? SwingWorker and its advantages.
Java Threads 11 Threading and Concurrent Programming in Java Introduction and Definitions D.W. Denbo Introduction and Definitions D.W. Denbo.
111 © 2002, Cisco Systems, Inc. All rights reserved.
 2004 Deitel & Associates, Inc. All rights reserved. 1 Chapter 4 – Thread Concepts Outline 4.1 Introduction 4.2Definition of Thread 4.3Motivation for.
1 GUI programming with threads. 2 Threads and Swing Swing is not generally thread-safe: most methods are not synchronized –correct synchronization is.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
OPERATING SYSTEM SUPPORT DISTRIBUTED SYSTEMS CHAPTER 6 Lawrence Heyman July 8, 2002.
An Introduction to Programming and Object Oriented Design using Java 3 rd Edition. Dec 2007 Jaime Niño Frederick Hosch Chapter 18 Integrating user interface.
Concurrent Programming and Threads Threads Blocking a User Interface.
Keeping your Swing Applications Responsive using FoxTrot and Friends Rob Ratcliff.
1 Object Oriented Programming Lecture XII Multithreading in Java, A few words about AWT and Swing, The composite design pattern.
Middleware Services. Functions of Middleware Encapsulation Protection Concurrent processing Communication Scheduling.
CSC Multiprocessor Programming, Spring, 2012 Chapter 7 – Cancellation & Shutdown Dr. Dale E. Parson, week 9-10.
Lecture 5: Threads process as a unit of scheduling and a unit of resource allocation processes vs. threads what to program with threads why use threads.
Before class… 下禮拜一交 Proposal – 至多四人一組, 同組同分 – 請勿超過一張 A4 評分標準 創意 技術難度 實用性 User-defined 完整性.
CSC Multiprocessor Programming, Spring, 2012 Chapter 11 – Performance and Scalability Dr. Dale E. Parson, week 12.
Swinging in Your Java Playground. Background Swing is a part of the Java Foundation Classes (JFC). The JFC is made up of features intended to give a programmer.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
CSC 322 Operating Systems Concepts Lecture - 7: by Ahmed Mumtaz Mustehsan Special Thanks To: Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
13-1 Chapter 13 Concurrency Topics Introduction Introduction to Subprogram-Level Concurrency Semaphores Monitors Message Passing Java Threads C# Threads.
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
Cs431-cotter1 Processes and Threads Tanenbaum 2.1, 2.2 Crowley Chapters 3, 5 Stallings Chapter 3, 4 Silberschaz & Galvin 3, 4.
Swing Threading Nested Classes COMP 401 Fall 2014 Lecture 23 11/25/2014.
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
Review IS Overview: Data  Inside the application Collections  Outside the application Database XML  Getting/displaying Swing  Communicating.
CHAPTER 6 Threads, Handlers, and Programmatic Movement.
Advanced Operating Systems CS6025 Spring 2016 Processes and Threads (Chapter 2)
Threads and Swing Multithreading. Contents I. Simulation on Inserting and Removing Items in a Combo Box II. Event Dispatch Thread III. Rules for Running.
Threads prepared and instructed by Shmuel Wimer Eng. Faculty, Bar-Ilan University 1July 2016Processes.
Java Thread Programming
Chapter 4 – Thread Concepts
CSC Multiprocessor Programming, Spring, 2012
Chapter 4 – Thread Concepts
Observer Design Pattern
Process Management Presented By Aditya Gupta Assistant Professor
Lecture 21 Concurrency Introduction
Lecture 28 Concurrent, Responsive GUIs
Operating Systems (CS 340 D)
Thread Implementation Issues
Android Topics Asynchronous Callsbacks
Chapter 3: Processes.
Chapter 4: Threads.
CS510 Operating System Foundations
Using threads for long running tasks.
CSC Multiprocessor Programming, Spring, 2011
Presentation transcript:

CSC Multiprocessor Programming, Spring, 2011 Chapter 9 – GUI Applications Dr. Dale E. Parson, week 11

Event Dispatch Thread GUI Events (user, timer etc.) occur in a GUI event dispatch thread. Method calls among Swing GUI objects must occur confined to this thread. No other thread my directly invoke methods on these GUI objects. The event dispatch thread should not invoke long- running computations outside the GUI. – The user interface becomes unresponsive.

Multithreaded GUI Attempts User actions bubble up from the O.S. Application actions bubble down to the O.S., e.g., to repaint a display. Resulting cycles in lock dependencies on thread-sage GUI components tends toward deadlock. Model-view-controller (MVC) pattern can suffer from the same problem. controller -> model -> view (model events paint view) controller -> view -> model (view queries model)

Thread Confinement for GUIs GUI events and updates are sequential. Burden of thread confinement is placed on application programmer. GUI components must be confined inside an encapsulated class or set of classes to maintain thread confinement. Simpler thread confinement to local variables and parameters is not possible because GUI components must outlive the call stack frames that construct them. GUI library must have hooks for interacting with other threads.

Thread confinement in Swing SwingUtilities.isEventDispatchThread – Is the current thread the AWT dispatch thread? SwingUtilities.invokeLater Invoke a Runnable on AWT dispatch thread after all pending events. invokeAndWait blocks until Runnable completes. Enqueuing repaint or revalidation request on the event queue can occur on any thread. Adding and removing event listeners can occur on any thread.

Simplest case Event responses either stay in GUI or interact with application objects using short-lived method invocations that are single-threaded or thread safe. Fast methods, no entry of other threads into GUI. GUI update can occur as a result of a change in an MVC model such as a TableModel (javax.swing.table) or TreeModel. Control must not leave the event thread.

Long running GUI tasks These must run in another thread. A cached thread pool is a good choice. User-initiated tasks normally do not grow in number unmanageably. A SingleThreadExecutor is good if you want to limit application concurrency without stalling the GUI. Only one application task runs at a time, but the GUI remains available for interrupting the task. On completion a task must submit a task on the dispatch thread (a Runnable) to update the UI.

Cancellation The user may want to abort a long running task from the GUI. You can interrupt the task directly, but … Submitting a task in an ExecutorService and then cancelling its Future, if necessary, is more organized. Set cancel’s mayInterruptIfRunning to true. Write task code to respond to interrupts.

SwingWorker javax.swing.SwingWorker is an abstract class (scaffolding) for building long-running computations driven from the GUI thread. It implements Runnable, Future, and RunnableFuture. Abstract method doInBackground runs in a background thread, returns T on completion. An application supplies this method, and SwingWorker takes care of propagating the result to the GUI thread.

Shared MVC models TableModel or TreeModel must update the View (GUI) on the dispatch thread. How to get results from another thread into the model? Use a thread-safe implementation of interface TableModel or TreeModel. Push data from the background task to the event thread via invokeLater, where the invoked Runnable updates the model. Let the event thread poll periodically for data from the worker thread.

Split data models A data model with both a presentation domain and an application domain is a split model design. Presentation model is confined to the event thread. Shared model is thread-safe and may be accessed by both the event thread and application threads. Presentation model registers listeners so it can be notified of updates. Event thread updates GUI with a snapshot of the model. Works best with small, infrequently updated model. If models is large or updates occur frequently, incremental updates of the mutations only (as opposed to complete snapshots) may be more efficient.

Split model, native code classes Consider a split-model design when a data model must be shared by more than one thread and implementing a thread-safe data model would be inadvisable because of blocking, consistency, or complexity reasons. Some native code libraries require all accesses to the library to be made by a single thread.