threads ii is 313 5.15.2003. outline quiz thread review stopping a thread java.util.timer swing...
TRANSCRIPT
![Page 1: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/1.jpg)
Threads II
IS 3135.15.2003
![Page 2: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/2.jpg)
Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads
javax.swing.Timer ProgressMonitor invokeLater
![Page 3: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/3.jpg)
Thread Review I Threads
Java objects Independent paths of execution Share memory and code
To define a thread’s behavior run()
![Page 4: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/4.jpg)
Threads Review II Threads only appear to run simultaneously
only a single thread executes at a time each thread runs for a time and then is
replaced by another Priority
determines which available thread is allowed to run
![Page 5: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/5.jpg)
Thread Review III Java event handling
takes place in a single thread other system threads
Threads may have resource conflict share the processor with sleep() and yield() achieve exclusive use through synchronized
methods coordinate using wait() and notify()
![Page 6: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/6.jpg)
Thread Review IV
![Page 7: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/7.jpg)
Thread Review V Lifecycle methods
start = transition to runnable end of run method = transition to dead transition to non-runnable
wait () sleep () blocked
transition back to runnable notify () end of sleep resource unlocked
![Page 8: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/8.jpg)
How to stop a thread run starts a thread
end of run method terminates what if I want to stop thread sooner?
Answer it depends
![Page 9: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/9.jpg)
Why not stop() Existing method in thread API Answer
Dead thread drops its locks synchronized method may be only partially executed corrupt state
![Page 10: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/10.jpg)
Examplepublic void run ()
{
while (true)
{
... do something ...
}
}
![Page 11: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/11.jpg)
Alternative I (exit variable)
private boolean m_isRunning;
public synchronized void setIsRunning (boolean newVal)
{ m_isRunning = newVal; }
public synchronized boolean IsRunning ()
{ return m_isRunning; }
public void run ()
{
setIsRunning(true);
while (isRunning())
{ ... do something ... }
}
![Page 12: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/12.jpg)
To Stop the Threadthread.setRunning (false)
![Page 13: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/13.jpg)
Requirements Inner loop Exit variable checked regularly
![Page 14: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/14.jpg)
Alternative II (interrupt)public void run ()
{
try
{
while (true)
{
foo.wait();
... do something ...
}
} catch (InterruptedException e)
{
.. clean up ...
}
}
![Page 15: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/15.jpg)
To Stop the Threadthread.interrupt ();
![Page 16: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/16.jpg)
Requirements Thread is in “wait” state in its inner loop
Also works for “sleep”
![Page 17: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/17.jpg)
Alternative III (i/o)public void run ()
{
try
{
while (true)
{
byte [] buffer = inputStream.readBytes(4000);
... do something ...
}
} catch (IOException e)
{
.. clean up ...
}
}
![Page 18: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/18.jpg)
To Stop the ThreadinputStream.close();
![Page 19: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/19.jpg)
Requirement Thread is waiting for I/O in its inner loop
![Page 20: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/20.jpg)
Three ways to stop Rapid inner loop
use a loop exit variable Wait state
call interrupt() use the interrupted exception
Waiting on I/O close the I/O stream use the IOException
![Page 21: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/21.jpg)
Periodic Action Examples
Check the mail server every 10 minutes Animate something on the screen Autosave
Need a thread that sleeps for a specified period then runs possibly repeats
![Page 22: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/22.jpg)
java.util.Timer Timer
schedule (TimerTask, delay) schedule (TimerTask, delay, period)
TimerTask implements Runnable
![Page 23: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/23.jpg)
java.util.Timer Lifecycle Timer created Task scheduled
wait setup in Timer thread Time arrives
TimerTask run method called (not a separate thread)
Timer canceled timer.cancel()
![Page 24: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/24.jpg)
javax.swing.Timer Timer
Timer (delay, ActionListener) ActionListener
actionPerformed (ActionEvent)
![Page 25: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/25.jpg)
javax.swing.Timer Lifecycle ActionListener created Timer created Timer started
timer.start() wait setup (in Timer thread)
Time arrives actionEvent created and inserted in event queue ActionListener handles event
Timer canceled timer.stop
![Page 26: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/26.jpg)
Progress Monitoring
![Page 27: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/27.jpg)
Example Add progress monitor to a process What needs to happen?
ProgressMonitor dialog must open Monitor must be updated Cancel/Completion must be handled
![Page 28: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/28.jpg)
ProgressMonitor class ProgressMonitor(Component parentComponent, Object
message, String note, int min, int max) setProgress
tell the dialog to change progress indication
![Page 29: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/29.jpg)
Example
![Page 30: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/30.jpg)
Note Updating must happen in EH thread
Swing timer ensures this If updating from another thread
must place updates into the EH thread
![Page 31: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/31.jpg)
Interaction between threads and UI Swing components are not thread-safe
methods not synchronized example
Solution only modify components from with the EH
thread after the component has been “realized”
![Page 32: Threads II IS 313 5.15.2003. Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor](https://reader035.vdocuments.net/reader035/viewer/2022062803/56649f575503460f94c7be29/html5/thumbnails/32.jpg)
Accessing the EH Thread EventQueue.invokeLater (Runnable) What happens
event inserted into event queue when it comes to top run method of object is called
Also invokeAndWait(Runnable) not as useful