1
15-214
School of Computer Science
Principles of Software Construction: Introduction to Multithreading - - PowerPoint PPT Presentation
Principles of Software Construction: Introduction to Multithreading and GUI Programming Christian Kaestner and Bogdan Vasilescu School of Computer Science 15-214 1 Administrivia Homework 4a due tonight Please follow naming conventions
1
15-214
School of Computer Science
2
15-214
3
15-214
4
15-214
5
15-214
6
15-214
– When an abstraction has two interdependent aspects and you want to reuse both – When state change to one
dependent on them
– Loose coupling between subject and observer, enhancing reuse – Support for broadcast communication – Notification can lead to further updates, causing a cascade effect
7
15-214
8
15-214
public void performAction(ActionEvent e) { List<String> lst = Arrays.asList(bar); foo.peek(42) } public void performAction(ActionEvent e) { bigBloatedPowerPointFunction(e); withANameSoLongIMadeItTwoMethods(e); yesIKnowJavaDoesntWorkLikeThat(e); } public void performAction(ActionEvent e) { List<String> lst = Arrays.asList(bar); foo.peek(40) }
9
15-214
10
15-214
11
15-214
12
15-214
13
15-214
14
15-214
Game Player Dealer newGame addCards addCards getAction action [action==hit] addCard
blocking execution
15
15-214
Game Player Dealer newGame addCards addCards hit addCard
16
15-214
GUI Framework OS Application
get event drawing commands next event event— mouse, key, redraw, …
17
15-214
– Describe how the GUI window should look – Use libraries for windows, widgets, and layout – Embed specialized code for later use – Register callbacks
– Framework gets raw events from OS (e.g., mouse clicks, key presses, window becomes visible) – Framework processes events (e.g., click at 10,40: which widget) – Triggers callback functions of corresponding widgets (if registered)
GUI Framework OS Application
get event drawing commands next event event— mouse, key, redraw, …
18
15-214
19
15-214
20
15-214
21
15-214
22
15-214
23
15-214
24
15-214
static List<String> cryptarithms(String[] words, int start, int end) { List<String> result = new ArrayList<>(); String[] tokens = new String[] {"", "+", "", "=", ""}; for (int i = start; i < end - 2; i++) { tokens[0] = words[i]; tokens[2] = words[i + 1]; tokens[4] = words[i + 2]; try { Cryptarithm c = new Cryptarithm(tokens); if (c.solve().size() == 1) result.add(c.toString()); } catch (IllegalArgumentException e) { // too many letters; ignore } } return result; }
25
15-214
public static void main(String[] args) { long startTime = System.nanoTime(); List<String> cryptarithms = cryptarithms(words, 0, words.length); long endTime = System.nanoTime(); System.out.printf("Time: %ds%n”, (endTime - startTime)/1e9); System.out.println(cryptarithms); }
26
15-214
public static void main(String[] args) throws InterruptedException { int n = Integer.parseInt(args[0]); // Number of threads long startTime = System.nanoTime(); int wordsPerThread = words.length / n; Thread[] threads = new Thread[n]; Object[] results = new Object[4]; for (int i = 0; i < n; i++) { // Create the threads int start = i == 0 ? 0 : i * wordsPerThread - 2; int end = i == n-1 ? words.length : (i + 1) * wordsPerThread; int j = i; // Only constants can be captured by lambdas threads[i] = new Thread(() -> { results[j] = cryptarithms(words, start, end); }); } for (Thread t : threads) t.start(); for (Thread t : threads) t.join(); long endTime = System.nanoTime(); System.out.printf("Time: %ds%n”, (endTime - startTime)/1e9); System.out.println(Arrays.toString(results)); }
27
15-214
28
15-214
29
15-214
30
15-214
31
15-214
32
15-214
33
15-214
34
15-214
35
15-214
36
15-214
37
15-214
public static void main(String[] args) { SwingUtilities.invokeLater(() -> new Test().setVisible(true)); }
38
15-214
39
15-214
40
15-214
– Put it in the window
– Use layouts to control positioning – Set up observers (a.k.a. listeners) to respond to events – Optionally, write custom widgets with application-specific display logic
41
15-214
//static public void main… JFrame window = … JPanel panel = new JPanel(); window.setContentPane(panel); JButton button = new JButton(“Click me”); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(“Button clicked”); } }); panel.add(button); window.setVisible(true);
42
15-214
//static public void main… JFrame window = … JPanel panel = new JPanel(); window.setContentPane(panel); JButton button = new JButton(“Click me”); button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.out.println(“Button clicked”); } }); panel.add(button); window.setVisible(true);
panel to hold the button
43
15-214
//static public void main… JFrame window = … JPanel panel = new JPanel(); window.setContentPane(panel); JButton button = new JButton(“Click me”); button.addActionListener((e) -> { System.out.println(“Button clicked”); } }); panel.add(button); window.setVisible(true);
44
15-214
interface ActionListener { void actionPerformed(ActionEvent e); }
class ActionEvent { int when; String actionCommand; int modifiers; Object source(); int id; … }
45
15-214
interface ActionListener { void actionPerformed(ActionEvent e); }
class ActionEvent { int when; String actionCommand; int modifiers; Object source(); int id; … }
class AbstractButton extends JComponent { private List<ActionListener> listeners; public void addActionListener(ActionListener l) { listeners.add(l); } protected void fireActionPerformed(ActionEvent e) { for (ActionListener l: listeners) l.actionPerformed(e); } }
46
15-214
interface ActionListener { void actionPerformed(ActionEvent e); }
class ActionEvent { int when; String actionCommand; int modifiers; Object source(); int id; … }
class AbstractButton extends JComponent { private List<ActionListener> listeners; public void addActionListener(ActionListener l) { listeners.add(l); } protected void fireActionPerformed(ActionEvent e) { for (ActionListener l: listeners) l.actionPerformed(e); } }
47
15-214
48
15-214
49
15-214
interface ActionListener { void actionPerformed(ActionEvent e); }
class ActionEvent { int when; String actionCommand; int modifiers; Object source(); int id; …
50
15-214
51
15-214
– Player clicks “hit” and expects a new card – When should the GUI update the screen? Game GUI update getData hit()
52
15-214
Game GUI update PointsPanel getData update getData update hit
53
15-214
Game GUI update PointsPanel getData update getData update hit
54
15-214
Game GUI update PointsPanel update(data) update(data) update hit
55
15-214
Game GUI update PointsPanel update(data) update(data) update hit
56
15-214
57
15-214
Game GUI register update PointsPanel notify notify update register hit
58
15-214
59
15-214
Manage inputs from user: mouse, keyboard, menu, etc. Manage display of information on the screen Manage data related to the application domain
60
15-214
http://msdn.microsoft.com/en-us/library/ff649643.aspx
61
15-214
– http://docs.oracle.com/javase/tutorial/uiswing/components/componentlist.html
– http://docs.oracle.com/javase/tutorial/uiswing/events/eventsandcomponents.html
62
15-214