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.

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Advertisements

18-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
28-Jun-15 Applets. 2 An applet is a program that is typically embedded in a Web page and can be run from a browser You need special HTML in the Web page.
29-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
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.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
CSE 331 Software Design & Implementation Dan Grossman Spring 2015 GUI Event-Driven Programming (Based on slides by Mike Ernst, Dan Grossman, David Notkin,
CC1007NI: Further Programming Week 5 Dhruba Sen Module Leader (Islington College)
CSE 380 – Computer Game Programming Render Threading Portal, by Valve,
Welcome to CIS 083 ! Events CIS 068.
Java Swing, Events and MVC Optional Readings: Eckel’s Thinking in Java: Chap 14 (
עקרונות תכנות מונחה עצמים תרגול 4 - GUI. Outline  Introduction to GUI  Swing  Basic components  Event handling.
Software Design 4.1 Tell, Don't Ask l Tell objects what you want them to do, do not ask questions about state, make a decision, then tell them what to.
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.
C H A P T E R T E N Event-Driven Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Dale Roberts GUI Programming using Java - Event Handling Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
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 GUI programming with threads. 2 Threads and Swing Swing is not generally thread-safe: most methods are not synchronized –correct synchronization is.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 13 : Swing I King Fahd University of Petroleum & Minerals College of Computer Science.
School of Computer Science & Information Technology G6DICP - Lecture 17 GUI (Graphical User Interface) Programming.
GUIs in Java Swing, Events CS2110, SW Development Methods Readings: MSD, Chapter 12 Lab Exercise.
Ch 3-4: GUI Basics Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Coming up: GUI Components.
Concurrent Programming and Threads Threads Blocking a User Interface.
SWING 101. IF YOU GET LOST - IMPORTANT LINKS  Swing articles:
Chapter 12Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Announcements/Reminders l Next week: »No Lectures »No Labs »Recitation.
Keeping your Swing Applications Responsive using FoxTrot and Friends Rob Ratcliff.
CS324e - Elements of Graphics and Visualization Java GUIs - Event Handling.
UID – Event Handling and Listeners Boriana Koleva
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Event-Driven Programming 1.
Advanced Concurrency Topics Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
Threads in Java1 Concurrency Synchronizing threads, thread pools, etc.
GUIs Basic Concepts. GUI GUI : Graphical User Interface Window/Frame : a window on the screen Controls/Widgets : GUI components.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
CSC Multiprocessor Programming, Spring, 2011 Chapter 9 – GUI Applications Dr. Dale E. Parson, week 11.
Creating a GUI Class An example of class design using inheritance and interfaces.
Graphical User Interfaces (GUI). PART ONE About GUI’s.
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
Java - hello world example public class HelloWorld { public static void main (String args[]) { System.out.println("Hello World"); }
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
CIS 270—Application Development II Chapter 11—GUI Components: Part I.
12-Jun-16 Event loops. 2 Programming in prehistoric times Earliest programs were all “batch” processing There was no interaction with the user Input Output.
Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent.
Threads and Swing Multithreading. Contents I. Simulation on Inserting and Removing Items in a Combo Box II. Event Dispatch Thread III. Rules for Running.
GUI Programming using Java - Event Handling
Events and Event Handling
Event loops 16-Jun-18.
CSE 331 Software Design & Implementation
CSE 331 Software Design & Implementation
Simulator of an Asynchronous Distributed System
Lecture 28 Concurrent, Responsive GUIs
CSE 331 Software Design and Implementation
Ellen Walker Hiram College
CSE 331 Software Design and Implementation
Event loops.
Android Topics UI Thread and Limited processing resources
Event loops 17-Jan-19.
Event loops 17-Jan-19.
Events, Event Handlers, and Threads
Constructors, GUI’s(Using Swing) and ActionListner
Event loops 8-Apr-19.
Tonga Institute of Higher Education
Threads.
Event loops.
CSE 331 Software Design & Implementation
Event loops.
Event loops 19-Aug-19.
TA: Nouf Al-Harbi NoufNaief.net :::
Presentation transcript:

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 I have analyzed/debugged a lot of GUI code Objects have proven a very productive way to look at GUI implementation  Problem: GUIs are made up of lots (and lots) of objects – buttons, menus, canvases, text areas, often overlayed on each other or implemented in terms of each other

Vulnerability  If GUI is multi-threaded, different threads access objects in different orders Bottom-up for arriving input Top-down for program output  Very difficult to design so that locks are acquired in the same order in all cases -- Deadlock!  Hence, general design principle that GUI objects should be thread-confined to a single, event thread In Java, AWT and Swing

The Basic Idea(s) Event loops – MSFC, original Mac, X Window  System puts arriving input events in a single, system- wide event queue (producer)  Application loop pulls an event from the queue, figures out what it means (often a big case statement) and processes it (consumer) Listener registration – AWT, Swing, Wx, …  Application registers code to process certain kinds of events  Internal toolkit loop calls all matching listeners for each event (in the event thread) Either way, GUI objects are only touched by the single, event thread, hence thread-confined

How does it work? Model-view-controller approach to GUI applications  Model: a collection of objects representing the state of the application  View: a collection of GUI objects showing the application on the screen  Controller: a collection of operations implementing changes to the state objects and GUI objects based on user input or program computation May be distributed between Model and View objects or separate

How does it work? (2) Arriving input event (associated with a GUI object) causes dispatcher to find listener and call a controller method, calls update Model method, calls update-View method All in one thread

Main problem: long-running listeners With only one thread handling events, what happens if one listener takes a long time?  Network I/O  File system I/O (local probably ok, but user can always sneak in a network connection these days)  Dialog boxes (so called modal dialogs) User has lost control of the system – can’t tell it to stop the long-running task; can’t get it to do anything else

Techniques for long-running listener tasks Perform them using a multi-threaded executor  Objects accessed (model) must be thread-safe  Can’t directly update the GUI To update the GUI  Put an event in the system event-queue and have a listener to handle it The listener runs in the event task! Nested (inner classes)/static scoping make this less ugly than it might be

Example: Listing 9.5 Button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { button.setEnabled(false); label.setText(“busy”); backgroundExec.execute(new Runnable() { public void run() { doBigComputation(); // long-running GuiExecutor.instance().execute(new Runnable() { public void run () { button.setEnabled(true); label.setText(“idle”); } }); }…