TCSS 305 Stepp - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

TCSS 305 Stepp

Description:

Stops timer from ticking. public void restart ... Returns true when timer is ticking (has been started). public void setDelay(int delay) ... – PowerPoint PPT presentation

Number of Views:125
Avg rating:3.0/5.0
Slides: 15
Provided by: tacomaWa
Category:
Tags: tcss | stepp | ticking

less

Transcript and Presenter's Notes

Title: TCSS 305 Stepp


1
TCSS 305 (Stepp)
  • Animation with Timers and Threads
  • Horstmann Ch. 6 pp. 223-225
  • Java Tutorial Timers, Threads.http//java.sun.co
    m/products/jfc/tsc/articles/timer/
  • http//java.sun.com/docs/books/tutorial/uiswing/mi
    sc/timer.html
  • http//java.sun.com/docs/books/tutorial/essential/
    threads/

2
Outline
  • timed events
  • Timer class
  • threads
  • multi-threaded programs
  • Thread class
  • Runnable interface

3
TimersWhy?
  • execute an action multiple timesat given
    intervals
  • create animations in our GUI programs
  • add delays and pauses when required / desired
  • a Timer is an example of a "callback" -- your
    code starts the timer, then later at a specified
    time, the timer activates, causing an event in
    your system

4
javax.swing.Timer
  • public Timer(int ms_delay, ActionListener
    al)Causes al to fire an actionPerformed every
    ms_delay milliseconds.
  • public void start()Causes timer to begin
    ticking the first tick happens after the initial
    delay, and the rest occur separated by the
    timer's delay.
  • public void stop()Stops timer from ticking.
  • public void restart()Restarts timer, causing it
    to wait its initial delay and then begin firing
    at its delay rate.

5
Timer more methods
  • public void addActionListener(ActionListener
    a)Adds additional listeners to be fired as the
    timer ticks.
  • public boolean isRunning()Returns true when
    timer is ticking (has been started).
  • public void setDelay(int delay)Changes the delay
    between timer ticks.
  • public void setInitialDelay(int delay)Sets a
    one-time delay to occur before ticking starts.
  • public void setRepeats(boolean b)Set to false to
    make timer fire only once.

6
Timer example
  • // This code could be used to move a shape to the
    right
  • // across the screen.
  • int DELAY 100
  • ActionListener updater new ActionListener()
  • public void actionPerformed(ActionEvent event)
  • x
  • myPanel.repaint()
  • Timer tim new Timer(DELAY, updater)
  • tim.start()

7
What is a Thread?
  • a "lightweight process" -- a single sequential
    flow of execution within a program
  • an isolated subtask inside a program
  • a means to implement programs that seem to
    perform multiple tasks simultaneously (a.k.a.
    concurrency, multitasking)

8
A multithreaded program
  • 2 or more threads program executes each thread
    sequentially, but interleaves themoverall
    program is concurrent
  • 1 thread program executes sequentially

9
Threads in Java Runnable
  • interface java.lang.Runnable
  • public void run()
  • Classes that implement Runnable can be wrapped up
    into Thread objects, which can then concurrently
    execute the Runnable's run method as a new thread

10
Threads in Java Thread
  • class java.lang.Thread
  • public Thread(Runnable toRun)creates a new
    thread to run the given object's code.
  • public void start()executes this thread (if
    based on a Runnable, calls its run method)
  • public void join()public void join(int ms)Waits
    the specified amount of time for the thread to
    finish.
  • public boolean isAlive()Returns whether this
    Thread is currently running.

11
Thread more methods
  • public void setPriority(int pri)Sets this
    thread's priority, which determines how much the
    thread tries to grab system resources, to the
    given amount. Examples are Thread.MAX_PRIORITY,
    Thread.MIN_PRIORITY, or Thread.NORM_PRIORITY.
  • public static void sleep(long ms) throws
    InterruptedExceptionCauses the currently active
    thread to stop running and give up control of the
    system for at least the specified number of
    milliseconds, after which time the thread will
    resume running.

12
Thread example using Runnable
  • class HelloRunnable implements Runnable
  • public void run()
  • while (true) // print every 500ms
  • try
  • Thread.sleep(500)
  • catch (InterruptedException ie)
  • System.out.println("Hello, world!")
  • // elsewhere
  • Thread ht2 new Thread(new HelloRunnable())
  • ht2.start()

13
Places threads are used
  • Graphical animation
  • javax.swing.Timer is preferred for this
  • event-handling loops
  • largely handled for us by Java
  • I/O
  • loading a file in the background
  • Networking
  • example thread that waits for another machine to
    connect

14
Thread issues
  • synchronization threads that are dependent on
    other threads may have to wait on each other
    (uses Java's monitors, synchronized, wait,
    notify...)
  • starvation a thread that uses all the CPU /
    memory resources and leaves none for the other
    threads
  • critical section problem what if 2 or more
    threads try to modify the same data
    simultaneously (example remove from an
    ArrayList) ?
Write a Comment
User Comments (0)
About PowerShow.com