25Thread3GUI

25Thread3GUI - CS108, Stanford Winter 2012 Handout #25...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS108, Stanford Handout #25 Winter 2012 Young Thread 3 GUI Handout written by Nick Parlante 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(), .
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/12/2012 for the course CS 108 taught by Professor Jimenez during the Winter '08 term at Stanford.

Page1 / 5

25Thread3GUI - CS108, Stanford Winter 2012 Handout #25...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online