Title: Last class review
1Last class review
- Event driven programming
- Event Listeners
- Adapters
- Abstract Actions
- Multithreading and Java Threads
- User and kernel threads
- Multiprocessing
- Javas main and event dispatch thread
2AbstractAction
String for menu item
Icon for button
- AbstractAction Example
- Action action new AbstractAction (Go Left,
new ImageIcon(images/left.gif) -
- public void actionPerformed(ActionEvent e)
-
- displayResult ()
-
- )
- JButton button new Jbutton(action)
- JMenuItem menuItem new JMenuItem(action)
- toolBar.add (button)
- menu.add (menuItem)
- ...
- action.setEnabled(false)
Shared action disables both
3Multithreaded Animation
- public class MyPanel extends JPanel implements
Runnable - int x 100
- Thread mythread
- public MyPanel() // constructor
- mythread new Thread(this)
- mythread.start()
-
- public void run()
- while(true) // animation loop
- x x 5
- repaint()
- Thread.sleep(100)
-
-
- public void paintComponent(Graphics g)
- super.paintComponent(g)
- Graphics2D g2 (Graphics2D)g
Event dispatch thread
4Cleaning/Stopping a Thread
- mythread null //will cleanup thread object
- We can use a flag set by a button for example to
stop a thread - boolean runMyAnimation
- while(runMyAnimation) //flag stops loop
- x x 5
- repaint() //calls paintComponent()
- Thread.sleep(100) //slow down animation
-
- ...
- mythreadnull// stops/cleans thread
5Controlling Animation Speed
- While(true)
- x x 5 //animation step size
- repaint()
- Thread.sleep(100)//delay between steps
-
Milliseconds 1000 1 second
6Swing Threads
Main thread
Event Dispatch thread
void main (..) JFrame framenew
JFrame() registerListeners() frame.show() //oth
er calculations ActionListener X
Registered Listeners
User events
Z
Y
X
Swing components are also painted/updated in
the event dispatch thread
UI Update
7Threads and Swing
- The rule for updating Swing
- Once a Swing component has been realised, all
code that might affect or depend on the state of
that component should be executed in the
event-dispatching thread - Thread safe mehods
- repaint()
- revalidate()
- invalidate()
- addXXXListener()
- removeXXXListener()
8InvokeLater
- invokeLater()
- requests that event thread runs certain code
- can be called from any thread
- code goes in run method of Runnable object
- returns immediately without waiting for event
thread to execute code. -
- Runnable updateAComponent new Runnable()
- public void run() component.doSomething()
-
- SwingUtilities.invokeLater(updateAComponent)
9InvokeAndWait
- invokeAndWait()
- identical to invokeLater() except doesnt return
till event thread has finished excecuting the
code. - Should use this if possible - less chance of
deadlock. - void showHelloThereDialog() throws Exception
- Runnable showModalDialog new Runnable()
- public void run()
JOptionPane.showMessageDialog(myMainFrame,
"Hello
There") -
-
- SwingUtilities.invokeAndWait(showModalDialog)
-
10SwingWorker
If code takes a lot of time to execute in event
dispatch thread we can use a SwingWorker
public void actionPerformed(ActionEvent) final
SwingWorker worker new SwingWorker() public
Object construct() //code that might take a
while to execute return someValue
worker.start()//required for SwingWorker 3
Not part of Java but avaiable
11Synchronizing Accesses to Shared Data
- Consider an Account class which provides debit
and credit methods which change the balance - If multiple Transaction threads want to access
the same Account object problems could arise
12"Atomic" Operations
- Operations that cannot be divided into "smaller"
operations - Example for myBalance being accessed by either
the debit() or credit() methods at about the same
time - these methods are not atomic at the byte-code
level - each has about four separate steps
13Non Atomic Operations in Separate Threads
- Assume credit() and debit() are in separate
threads - Both will
- read the balance
- create a temp value
- add to (or subtract from) the temp value
- replace the old balance with the temp value
- If each step is happening at about the same time
- each replaces the old balance with its own temp
value without knowing about the action of the
other, giving an incorrect new balance value !!!
14Making Methods Atomic
- Use the keyword synchronized to declare each
method that accesses the critical information - class Account. . .synchronized public void
debit(double amount) synchronized
public void credit(double amount
private myBalance
15Making Methods Atomic
- When a thread
- sends a synchronized message to an object
- and no other thread is executing a synchronized
method on that object - Then
- Java locks the object
- other synchronized methods cannot access the
object until the first terminates
16Issues in Multithreading
- Scheduling
- Priorities
- Deamon threads
- Synchronization
17The GUI equation
- GUI Containers Layout Widgets Event
Handlers Multiple Threads
18GUI Design
- Application classes should not depend on the GUI
- Parts of the application may be used by other
GUIs - Model View Controller Architecture (MVC) supports
separation of GUI from application classes