Threads - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Threads

Description:

One approach is to divide the problem into smaller tasks, and ... contain a stop method, this method is deprecated and should not be used to stop ... – PowerPoint PPT presentation

Number of Views:35
Avg rating:3.0/5.0
Slides: 18
Provided by: ayse7
Category:

less

Transcript and Presenter's Notes

Title: Threads


1
Threads
  • Doing Two or More Tasks At Once

2
Multitasking (or multiprogramming)
  • Given a problem
  • One approach is to divide the problem into
    smaller tasks, and
  • According to the nature of sub-tasks and the
    sequence in which the sub-tasks must be done,
    assign each sub-task to a saparate process, and
  • If needed have the processes to communicate with
    each other.
  • This is the basis for distributed programming.
  • Examples GUIs, Matrix multiplication

3
Threads
  • However, generating and managing processes
    consumes system resources.
  • Threads are a solution for parallel programming
    which are not system intensive as saparate
    processes
  • A threadsometimes called an execution context or
    a lightweight processis a single sequential flow
    of control within a program.

4
Threads vs. Processes
  • Threads
  • A thread has no data segment or heap
  • A thread cannot live on its own, it must leave
    within a processes.
  • There can be more than one thread in a process.
  • Inexpensive creation
  • In expensive context switching

5
Threads vs. Processes
  • Processes
  • A process has code/data/heap and other segments
  • Threads within a process share code/data/heap,
    share I/O, but each has its own stack registers
  • Expensive creation
  • Expensive context swiching

6
An Example
  • A Web browser is an example of a multithreaded
    application.
  • Within a typical browser,
  • you can scroll a page while its downloading an
    applet or an image,
  • play animation and sound concurrently,
  • print a page in the background while you download
    a new page,
  • or watch three sorting algorithms race to the
    finish.
  • Try to imagine a web browser which is not
    multithreaded.

7
Implementing a Thread
  • There are two ways to implement a thread
  • Subclassing Thread and Overriding run
  • Implementing the Runnable Interface

8
Subclassing Thread and Overriding run
  • Example code

9
Implementing the Runnable Interface
  • Example Code (Clock Applet)
  • The Clock applet uses a technique different from
    SimpleThreads for providing the run method for
    its thread. Instead of subclassing Thread, Clock
    implements the Runnable interface and therefore
    implements the run method defined in it. Clock
    then creates a thread with itself as the Threads
    target. When created in this way, the Thread gets
    its run method from its target.
  • Deciding to use the runnable interface
  • For most cases, including that of the Clock
    applet, if your class must subclass some other
    class (the most common example being Applet), you
    should use Runnable.

10
The Life Cycle of a Thread
11
Creating a Thread
  • clockThread new Thread(this, "Clock")
  • After the highlighted statement has been
    executed, clockThread is in the New Thread state.
    A thread in this state is merely an empty Thread
    object no system resources have been allocated
    for it yet. When a thread is in this state, you
    can only start the thread.

12
Starting a Thread
  • clockThread.start()
  • The start method creates the system resources
    necessary to run the thread, schedules the thread
    to run, and calls the thread's run method.
    clockThread's run method is defined in the Clock
    class.
  • Many computers have a single processor, thus
    making it impossible to run all "running" threads
    at the same time. So at any given time, a
    "running" thread may be waiting for its turn in
    the CPU.

13
Making a Thread Not Runnable
  • A thread becomes Not Runnable when one of these
    events occurs
  • Its sleep method is invoked.
  • The thread calls the wait method to wait for a
    specific condition to be satisifed.
  • The thread is blocking on I/O.

14
Stopping a Thread
  • Although the Thread class does contain a stop
    method, this method is deprecated and should not
    be used to stop a thread because it is unsafe.
    Rather, a thread should arrange for its own death
    by having a run method that terminates naturally.

15
Testing Thread State
  • The API for the Thread class includes a method
    called isAlive.
  • The isAlive method returns true if the thread has
    been started and not stopped. If the isAlive
    method returns false, you know that the thread
    either is a New Thread or is Dead. If the isAlive
    method returns true, you know that the thread is
    either Runnable or Not Runnable. You cannot
    differentiate between a New Thread or a Dead
    thread. Nor can you differentiate between a
    Runnable thread and a Not Runnable thread.

16
Other Topics
  • Thread Scheduling
  • Synchronizing Threads
  • Thread Pools
  • and much more

17
Labwork
  • Implement a stopwatch as follows
  • There should be a GUI which shows the elapsed
    time.
  • There should be button to start and stop the stop
    watch.
Write a Comment
User Comments (0)
About PowerShow.com