CS108, Stanford Handout #22. Thread 3 GUI

Similar documents
CS193j, Stanford Handout #21. Threading 3

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

Topic 9: Swing. Swing is a BIG library Goal: cover basics give you concepts & tools for learning more

Topic 9: Swing. Why are we studying Swing? GUIs Up to now: line-by-line programs: computer displays text user types text. Outline. 1. Useful & fun!

CS 251 Intermediate Programming GUIs: Event Listeners

G51PGP Programming Paradigms. Lecture 009 Concurrency, exceptions

CS193k, Stanford Handout #10. Threads 4 / RMI

Outline. Topic 9: Swing. GUIs Up to now: line-by-line programs: computer displays text user types text AWT. A. Basics

G51PGP Programming Paradigms. Lecture 008 Inner classes, anonymous classes, Swing worker thread

Frames, GUI and events. Introduction to Swing Structure of Frame based applications Graphical User Interface (GUI) Events and event handling

AP CS Unit 11: Graphics and Events

The JFrame Class Frame Windows GRAPHICAL USER INTERFACES. Five steps to displaying a frame: 1) Construct an object of the JFrame class

CS193k, Stanford Handout #8. Threads 3

OOP Assignment V. For example, the scrolling text (moving banner) problem without a thread looks like:

Multiple Choice Questions: Identify the choice that best completes the statement or answers the question. (15 marks)

Swing: Building GUIs in Java

Swing: Building GUIs in Java

H212 Introduction to Software Systems Honors

1.00 Lecture 29. Sensors and Threads. Reading for next time: Numerical Recipes (online)

Animation Part 2: MoveableShape interface & Multithreading

CS193k, Stanford Handout #16

(Incomplete) History of GUIs

CS193j, Stanford Handout #36.5. CS193j Solution

Building a GUI in Java with Swing. CITS1001 extension notes Rachel Cardell-Oliver

Graphical User Interface (GUI)

GUI Forms and Events, Part II

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Final Exam CS 251, Intermediate Programming December 13, 2017

CS108, Stanford Handout #33. Sockets

CS108, Stanford Handout #19. Swing GUI

HW#1: Pencil Me In Status!? How was Homework #1? Reminder: Handouts. Homework #2: Java Draw Demo. 3 Handout for today! Lecture-Homework mapping.

Lecture 3: Java Graphics & Events

RAIK 183H Examination 2 Solution. November 10, 2014

Graphical User Interfaces (GUIs)

JRadioButton account_type_radio_button2 = new JRadioButton("Current"); ButtonGroup account_type_button_group = new ButtonGroup();

PROGRAMMING DESIGN USING JAVA (ITT 303) Unit 7

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

MIT AITI Swing Event Model Lecture 17

Graphical User Interface (GUI)

Lecture 5: Java Graphics

Goals. Lecture 7 More GUI programming. The application. The application D&D 12. CompSci 230: Semester JFrame subclass: ListOWords

Object-Oriented Software Engineering Re-exam, 2012 (Also Object-Oriented Analysis, Design and Programming, Re-exam, 2012)

CS 251 Intermediate Programming GUIs: Components and Layout

CSC207 Week 4. Larry Zhang

Course Status Networking GUI Wrap-up. CS Java. Introduction to Java. Andy Mroczkowski

Graphic Interface Programming II Events and Threads. Uppsala universitet

RAIK 183H Examination 2 Solution. November 11, 2013

Introduction to GUIs. Principles of Software Construction: Objects, Design, and Concurrency. Jonathan Aldrich and Charlie Garrod Fall 2014

[module lab 2.2] GUI FRAMEWORKS & CONCURRENCY

Java for Interfaces and Networks (DT3010, HT10)

Introduction to Graphical User Interfaces (GUIs) Lecture 10 CS2110 Fall 2008

Java Programming Lecture 6

CPS122 Lecture: Graphical User Interfaces and Event-Driven Programming

CS180 Spring 2010 Exam 2 Solutions, 29 March, 2010 Prof. Chris Clifton

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

Class 34: Introduction to Threads

Midterm assessment - MAKEUP Fall 2010

1.00/1.001 Introduction to Computers and Engineering Problem Solving Final Examination - December 15, 2003

Java for Interfaces and Networks (DT3029)

SE1021 Exam 2. When returning your exam, place your note-sheet on top. Page 1: This cover. Page 2 (Multiple choice): 10pts

Queen s University Faculty of Arts and Science School of Computing CISC 124 Final Examination December 2004 Instructor: M. Lamb

Swing from A to Z Some Simple Components. Preface

CS Exam 3 - Spring 2010

CS Exam 3 - Spring 2010

Part I: Learn Common Graphics Components

Programming Languages and Techniques (CIS120)

GUI and its COmponent Textfield, Button & Label. By Iqtidar Ali

Interface Implementation

CS Exam 1 Review Suggestions

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

Advanced Java Unit 6: Review of Graphics and Events

CS180 Review. Recitation Week 15

SINGLE EVENT HANDLING

Multimedia Programming

CS193k, Stanford Handout #12. Threads 4 / RMI

Graphic User Interfaces. - GUI concepts - Swing - AWT

Programming Language Concepts: Lecture 8

Window Interfaces Using Swing Objects

Multiprocessing Threads/Lightweight objects Thread methods Concurrency Issues

Computer Science 210: Data Structures. Intro to Java Graphics

AP CS Unit 12: Drawing and Mouse Events

CS211 GUI Dynamics. Announcements. Motivation/Overview. Example Revisted

Lab. Lecture 26: Concurrency & Responsiveness. Assignment. Maze Program

Introduction. Introduction

Multiprocessing Threads/Lightweight objects Thread methods Concurrency Issues

Introduction to concurrency and GUIs

Graphical interfaces & event-driven programming

Windows and Events. created originally by Brian Bailey

Programmierpraktikum

17 GUI API: Container 18 Hello world with a GUI 19 GUI API: JLabel 20 GUI API: Container: add() 21 Hello world with a GUI 22 GUI API: JFrame: setdefau

Java, Swing, and Eclipse: The Calculator Lab.

Proctors are unable to respond to queries about the interpretation of exam questions. Do your best to answer exam questions as written.

Introduction to the JAVA UI classes Advanced HCI IAT351

Window Interfaces Using Swing Objects

Java: Graphical User Interfaces (GUI)

DCS235 Software Engineering Exercise Sheet 2: Introducing GUI Programming

Swing - JTextField. Adding a text field to the main window (with tooltips and all)

Threads, Concurrency, and Parallelism

Graphical User Interfaces 2

CPS221 Lecture: Threads

Transcription:

CS108, Stanford Handout #22 Winter, 2006-07 Nick Parlante Thread 3 GUI GUIs and Threading Problem: Swing vs. Threads How to integrate the Swing/GUI/drawing system with threads? Problem: The GUI system -- components, buttons, frames, etc. -- is a big data structure which must support many read and write operations. Cannot have two or more thread manipulating that data structure at the same time (unless they are all just reading it). e.g. read/write conflict if paintcomponent() reads the GUI state while another thread changes it Solution: Swing Thread / Event Queue aka One Big Lock There is one, designated official "Swing thread" - Also knows as the "draw thread" or the "UI thread" or the "event thread" The system does all Swing/GUI notifications using the Swing thread, one at a time.. - paintcomponent() -- always called on the Swing thread - All notifications -- action events, mouse motion events -- are called on the Swing thread. e.g. actionperformed() is called on the Swing thread. The system keeps a queue of "Event jobs". When the swing thread is done with its current job, it loops around and gets the next one and does it. The Swing thread does one thing at a time. It is approximately FIFO, but not exactly. For example, the Swing thread might process button clicks ahead of draw requests. Only the swing thread is allowed to access (read or write) the state of the GUI -- the labels of things, the geometry, the nesting, the listeners, etc. of the swing components. e.g. gettext(), setlayout(),... all those can only be called by the swing thread. This is a big constraint -- only the Swing thread may touch swing objects. Since only the Swing thread is allowed to access swing state, in effect there is one big lock over all the swing state. Common GUI Thread Solution This solution -- identifying a special thread which exclusively "owns" all the GUI state -- is the most common way to integrate a GUI with threading. It is probably the simplest, best way to combine a GUI with threading. Programmer Rules... 1. On the swing thread -- using Swing is ok When you are on the Swing thread, you are allowed to using Swing all you want. All paint and control notifications sent to your code -- paintcomponent(), actionperformed(),... -- these are sent on the Swing thread, so you are fine in those cases. e.g. fine to call container.add(), setpreferredsize(), setlayout(), gettext(), settext() 2. Don't hog the swing thread Do not do time-consuming operations on the swing thread

2 There is only one swing thread, and while you are running on it, it cannot do any of the normal housekeeping work of the GUI. No Swing/GUI event processing, drawing, etc. will happen until you finish your processing and return the swing thread to the system -- then it can de-queue drawing requests, mouse events, push in buttons, etc. - Note in the example below how "Homework Cancelled" never appears on screen. "Returning the swing thread to the system" - e.g. your code is actionperformed() { foo(); bar();, running on the Swing thread - After foo() and bar() are done and the method exits its last... this returns the Swing thread to dequeue the next task. Fork off a worker thread to do a time-consuming operation, leaving the Swing thread to maintain the GUI. 3. Worker not on the swing thread -- no touch Swing state A thread which is not the swing thread may not send messages that use the swing state (add(), gettext(), settext(), setbounds(),...). Instead, use invokelater() (below) to run code on the Swing thread Violating this rule will appear to work. However, the violation is a latent bug/race-condition between your thread and the Swing thread, waiting to screw up the data someday. Exceptions: a few rare swing methods use locks internally so that they can be called by any thread -- they are "thread safe". Thread safe methods include repaint(), revalidate(), and the add/remove Listener methods. Finally, as a 1-off exception, settext() of the JTextField is thread-safe. Overall, the vast majority of Swing methods are not thread safe. Another exception is before the component has been brought on screen (setvisible(true)) -- before the component is on screen, the swing thread is not using it, so it's ok to add(), setbounds(), etc. on it. - This is often used in main()... set up Swing state (not on the Swing thread), and call setvisible(true) as the last step - The official best practice for Java is being revised so the main()/setvisible(trick) is no longer recommended. - Instead, main could look like: SwingUtilities.invokeLater( new Runnable() { public void run() { -- do it here-- ); - This new requirement is not a client friendly design -- make common things easy, remember! I hope they come up with a better way for no-brainer client code to bring up a GUI simply. SwingUtilities Built in utility methods that allow you to "post" some code to the swing thread to run later. "Runnable" interface -- defines public void run() { SwingUtilities.invokeLater(Runnable) - Queue up the given Runnable -- the Swing thread will execute the Runnable when the Swing thread gets to it in the queue of things to do. SwingUtilities.invokeAndWait(Runnable) - As above, but block the current thread (a worker thread of some sort presumably), until the Runnable exits. In Java 5, this class is now called "EventQueue" instead of "SwingUtilities" but the functionality is the same. SwingThread Demo Demonstrates swing thread issues Hogging the swing thread -- bad! Fork off a worker + worker uses SwingUtilities.invokeLater() to communicate back to the swing state

3 // SwingThread.java import java.awt.*; import javax.swing.*; import java.awt.event.*; /* Demonstrates using workers and the swing thread, and interruption. */ class SwingThread extends JFrame { private JLabel label; private JButton right; private JButton wrong; private JButton fork; private JTextField field; private LabelWorker worker; private JButton inter; public SwingThread() { super("swing Thread"); setlayout( new BoxLayout(getContentPane(), BoxLayout.Y_AXIS) ); // put in a label label = new JLabel("hello"); add(label); // Button that adds an "x" when clicked -- fine, // runs on the swing thread right = new JButton("Add Right x"); add(right); right.addactionlistener(new ActionListener() { label.settext(label.gettext() + " x"); ); // Bad -- hogs the Swing thread // the whole GUI will appear to lock up for 5 seconds -- use worker thread instead // Note also that the "All Homework Is Cancelled" NEVER appears on screen wrong = new JButton("Add Wrong y"); add(wrong); wrong.addactionlistener(new ActionListener() { String text = label.gettext(); label.settext("all Homework Is Cancelled!"); // sleep for 5 seconds -- simulate time consuming operation try { Thread.sleep(5000); catch (InterruptedException ignored) { label.settext(text + " y");

4 ); // Field appends text to label -- uses worker thread correctly field = new JTextField("hi", 20); field.setmaximumsize(new Dimension(200, 20)); add(field); worker = null; fork = new JButton("Fork Off Adder"); add(fork); // fork button -> set text, then fork off worker fork.addactionlistener(new ActionListener() { // set text right away -- ok, we're on the swing thread label.settext(label.gettext() + "... WAIT FOR IT!"); // Interrupt previous worker if it exists if (worker!= null) worker.interrupt(); ); // fork off new worker using text from field worker = new LabelWorker(field.getText()); worker.start(); // Interrupt existing worker inter = new JButton("Interrupt"); add(inter); inter.addactionlistener(new ActionListener() { // Q: Is there a race condition between this code and // the above worker interrupt code? if (worker!= null) { worker.interrupt(); worker = null; ); setdefaultcloseoperation(jframe.exit_on_close); pack(); setvisible(true);

5 // Takes a word, appends it 10 times to the label, // working very slowly. Exits when interrupted private class LabelWorker extends Thread { String word; public LabelWorker(String initword) { word = initword; public void run() { String text = ""; for (int i = 0; i < 10; i++) { // Sleep for a second, exit on interrupt try { Thread.sleep(1000); catch (InterruptedException ex) { // Control goes here if we are interrupted while sleeping -- // exit the run loop. (Way 1 to notice interruption) break; // Do some computation, storing in a final temp variable, // so it is visible in the Runnable text = text + word; final String finaltext = text; // Notice if interrupted -- exit // (Way 2 to notice interruption) if (isinterrupted()) break; // NO NO NO, cannot do this // label.settext(finaltext); // Message back to the GUI using invokelater/runnable SwingUtilities.invokeLater(new Runnable() { public void run() { label.settext(finaltext); ); public static void main(string[] args) { new SwingThread();