DOC PREVIEW
DePaul IS 313 - Thread II

This preview shows page 1-2-15-16-31-32 out of 32 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 32 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Threads IIOutlineThread Review IThreads Review IIThread Review IIIThread Review IVThread Review VHow to stop a threadWhy not stop()ExampleAlternative I (exit variable)To Stop the ThreadRequirementsAlternative II (interrupt)To Stop the ThreadRequirementsAlternative III (i/o)To Stop the ThreadRequirementThree ways to stopPeriodic Actionjava.util.Timerjava.util.Timer Lifecyclejavax.swing.Timerjavax.swing.Timer LifecycleProgress MonitoringExampleProgressMonitor classExampleNoteInteraction between threads and UIAccessing the EH ThreadThreads IIIS 3135.15.2003Outline Quiz Thread review Stopping a thread java.util.Timer Swing threads javax.swing.Timer ProgressMonitor invokeLaterThread Review I Threads Java objects Independent paths of execution Share memory and code To define a thread’s behavior run()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 runThread 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()Thread Review IVThread 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 unlockedHow to stop a thread run starts a thread end of run method terminates what if I want to stop thread sooner? Answer it dependsWhy not stop() Existing method in thread API Answer Dead thread drops its locks synchronized method may be only partially executed corrupt stateExamplepublic void run (){while (true){... do something ...}}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 ... }}To Stop the Threadthread.setRunning (false)Requirements Inner loop Exit variable checked regularlyAlternative II (interrupt)public void run (){try{while (true){foo.wait();... do something ...}} catch (InterruptedException e){.. clean up ...}}To Stop the Threadthread.interrupt ();Requirements Thread is in “wait” state in its inner loop Also works for “sleep”Alternative III (i/o)public void run (){try{while (true){byte [] buffer = inputStream.readBytes(4000);... do something ...}} catch (IOException e){.. clean up ...}}To Stop the ThreadinputStream.close();Requirement Thread is waiting for I/O in its inner loopThree 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 IOExceptionPeriodic 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 repeatsjava.util.Timer Timer schedule (TimerTask, delay) schedule (TimerTask, delay, period) TimerTask implements Runnablejava.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()javax.swing.Timer Timer Timer (delay, ActionListener) ActionListener actionPerformed (ActionEvent)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.stopProgress MonitoringExample Add progress monitor to a process What needs to happen? ProgressMonitor dialog must open Monitor must be updated Cancel/Completion must be handledProgressMonitor class ProgressMonitor(Component parentComponent, Object message, String note, int min, int max)  setProgress tell the dialog to change progress indicationExampleNote Updating must happen in EH thread Swing timer ensures this If updating from another thread must place updates into the EH threadInteraction 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”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


View Full Document

DePaul IS 313 - Thread II

Download Thread II
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Thread II and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Thread II 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?