Threads II IS 313 5.15.2003. Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
Multi-threaded applications SE SE-2811 Dr. Mark L. Hornick 2 What SE1011 students are told… When the main() method is called, the instructions.
CS 5704 Fall 00 1 Monitors in Java Model and Examples.
Multithreading Horstmann ch.9. Multithreading Threads Thread states Thread interruption Race condition Lock Built-in lock java.util.concurrent library.
Gots ta move dat data and not trash your threads...
Thread synchronization Example:Producer/Consumer Relationship Buffer –Shared memory region Producer thread –Calls produce method to add item to buffer.
Multithreading The objectives of this chapter are:
Threads Daniel Bennett, Jeffrey Dovalovsky, Dominic Gates.
G52CON: Concepts of Concurrency Lecture 2 Processes & Threads Chris Greenhalgh School of Computer Science
(c) 2006 E.S.Boese All Rights Reserved. Threads and Timers Chapter 19 - Student.
13-Jun-15 Animation. 2 Moving pictures Animation—making objects that appear to move on the screen—is done by displaying a series of still pictures, one.
Object-Oriented Software Engineering Concurrent Programming.
Advanced Java Class Threads.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
50.003: Elements of Software Construction Week 5 Basics of Threads.
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
Java Programming: Advanced Topics
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
1 CSCE3193: Programming Paradigms Nilanjan Banerjee Programming Paradigms University of Arkansas Fayetteville, AR
MODERN OPERATING SYSTEMS Third Edition ANDREW S. TANENBAUM Chapter 2 Processes and Threads Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall,
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Threads in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 GUI programming with threads. 2 Threads and Swing Swing is not generally thread-safe: most methods are not synchronized –correct synchronization is.
1 Web Based Programming Section 8 James King 12 August 2003.
Internet Software Development Controlling Threads Paul J Krause.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
ICS 313: Programming Language Theory Chapter 13: Concurrency.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Multithreading in JAVA
Threads Opening Discussion zWhat did we talk about last class? zDo you have any questions about the assignment?
Java Thread and Memory Model
Threads Doing Several Things at Once. Threads n What are Threads? n Two Ways to Obtain a New Thread n The Lifecycle of a Thread n Four Kinds of Thread.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Thread A thread represents an independent module of an application that can be concurrently execution With other modules of the application. MULTITHREADING.
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
Review IS Overview: Data  Inside the application Collections  Outside the application Database XML  Getting/displaying Swing  Communicating.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Java Thread Programming
Multithreading The objectives of this chapter are:
Threads in Java Two ways to start a thread
Multithreading / Concurrency
CSE 501N Fall ‘09 21: Introduction to Multithreading
Multithreaded Programming in Java
Multithreading Chapter 9.
Threads Chate Patanothai.
Threads II IS
MODERN OPERATING SYSTEMS Third Edition ANDREW S
Condition Variables and Producer/Consumer
Multithreading.
Condition Variables and Producer/Consumer
Multithreading 2 Lec 24.
9. Threads SE2811 Software Component Design
Threads in Java James Brucker.
Multithreading in java.
9. Threads SE2811 Software Component Design
Multithreading The objectives of this chapter are:
9. Threads SE2811 Software Component Design
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Threads II IS

Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor  invokeLater

Thread 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 run

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()

Thread Review IV

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

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

Why not stop()  Existing method in thread API  Answer Dead thread drops its locks  synchronized method may be only partially executed  corrupt state

Example public 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 Thread thread.setRunning (false)

Requirements  Inner loop  Exit variable checked regularly

Alternative II (interrupt) public void run () { try { while (true) { foo.wait();... do something... } } catch (InterruptedException e) {.. clean up... }

To Stop the Thread thread.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 Thread inputStream.close();

Requirement  Thread is waiting for I/O in its inner loop

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

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

java.util.Timer  Timer schedule (TimerTask, delay) schedule (TimerTask, delay, period)  TimerTask implements Runnable

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()

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.stop

Progress Monitoring

Example  Add progress monitor to a process  What needs to happen? ProgressMonitor dialog must open Monitor must be updated Cancel/Completion must be handled

ProgressMonitor class  ProgressMonitor(Component parentComponent, Object message, String note, int min, int max)  setProgress tell the dialog to change progress indication

Example

Note  Updating must happen in EH thread Swing timer ensures this  If updating from another thread must place updates into the EH thread

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”

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