Cup 9: Threads - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Cup 9: Threads

Description:

... the thread for execution (it is in the runnable state) the thread's run() method ... A Thread has a stop() method that can also be used to terminate it ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 16
Provided by: timma87
Category:
Tags: cup | thread | threads

less

Transcript and Presenter's Notes

Title: Cup 9: Threads


1
Cup 9 Threads
  • Special Topics Java

Dr. Tim Margush Department of Mathematics and
Computer Science The University of Akron
2
Whats a Thread?
  • Thread of Control
  • A sequential flow of control within a program
  • Threads are distinct execution sequences
    occurring simultaneously within one programs
    memory space
  • Allows efficient usage of single or multiple
    processors

3
Obtaining a New Thread
  • Define a class that
  • extends java.lang.Thread
  • overrides the run() method
  • Instantiate an object of that class
  • this creates a new thread
  • Define a class that
  • implements Runnable
  • implements the run() method
  • Instantiate an object of that class
  • this creates a new thread

4
Starting a Thread
  • start() //duh!
  • before starting, a thread does not use any system
    resources
  • start schedules the thread for execution (it is
    in the runnable state)
  • the threads run() method is what gets executed
  • the thread may yield to other threads, but
    continues to execute when possible

5
Not Runnable?
  • A Thread becomes Not Runnable if
  • its sleep method is called
  • becomes runnable after the specified time period
    has elapsed
  • it calls a wait method
  • becomes runnable when notified that the wait
    condition has been met
  • it is blocked on I/O
  • becomes runnable when the IO finishes

6
Stop Thread! Stop!
  • A Thread stops (becomes dead) when its run method
    terminates
  • A Thread has a stop() method that can also be
    used to terminate it
  • throws a ThreadDeath error which ultimately
    causes the termination
  • do not catch the error or the Thread will not die
    (unless you re-throw it)

7
User and Daemon Threads
  • Daemon Thread
  • a subordinate thread
  • an application will halt when only Daemon Threads
    are left running
  • setDaemon(true)
  • call before starting the Thread
  • User Thread
  • independent from the creating Thread
  • should be designed with some stopping criterion
  • Threads created from Daemon threads are Daemons
    by default

8
Let Sleeping Threads Lie
  • Thread method sleep(long millisec)
  • Suspends Thread execution for specified time -
    allowing other Threads to run
  • May throw an InterruptedException
  • caused by calling the Threads interrupt() method
  • this wakes up a sleeping thread
  • the exception should be caught
  • sleep(long millisecs, long nanosecs)

9
Yielding to Another Thread
  • Thread method yield()
  • allows waiting Threads a chance to execute
  • allows current Thread to continue if other
    Threads are not able to run

10
Scheduling Conflicts
  • Thread method setPriority(int)
  • higher priority Threads will execute first (if
    several Threads are ready to run)
  • low priority Threads will be preempted if a
    higher priority Thread becomes runnable
  • equal priority Threads may share processor
    resources (time-slice)
  • system dependent

11
Joining Threads
  • aThread.join()
  • suspends the current Thread until the named
    Thread dies
  • you may specify a maximum time to wait
  • InterruptedException may occur

12
Thread Relatives
  • Unsynchronized Threads
  • should not use the same resources
  • no need to communicate between Threads
  • Synchronized Threads
  • must access shared resources, probably not at the
    same time
  • may need to communicate with each other

13
Synchronization
  • synchronized method_a()...
  • synchronized method_b()...
  • Two Threads cannot be executing in these methods
    on the same object at the same time
  • different objects - OK
  • If methods are not synchronized, different
    Threads can run in them at the same time (even
    within a single method and even if a synchronized
    method is executing)

14
InterThread Communication
  • anObject.wait()
  • suspends the current Thread that is synchronized
    on this object
  • You may specify a maximum wait time (in
    milliseconds)
  • releases the Thread's lock
  • the current Thread must have the lock on the
    object, or this will fail
  • InterruptedException is possible

15
Notifying Waiting Threads
  • anObject.notify()
  • anObject.notifyAll()
  • Places one (or all) Threads waiting for a lock on
    this object in the runnable state
  • This may occur automatically if the maximum wait
    time has elapsed
  • The current Thread must have a lock on this
    object
Write a Comment
User Comments (0)
About PowerShow.com