ICS11 Introduction to Computer Programming II - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

ICS11 Introduction to Computer Programming II

Description:

Joselito dela Cruz Ty. Java sa Eskwela (JsE) Program. Xavier University Computer Center ... Either extend class Thread or implement interface Runnable. e.g. ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 24
Provided by: jayrobertc
Category:

less

Transcript and Presenter's Notes

Title: ICS11 Introduction to Computer Programming II


1
Xavier University Computer Center Sun
Microsystems, Philippines
Java sa Eskwela (JsE) Program
  • ICS11 Introduction to Computer Programming II
  • Joselito dela Cruz Ty

2
Chapter 15
Multithreading Performing Several Tasks
Concurrently
3
Outline
  • Multitasking Concepts
  • Multithreading Concepts
  • Thread Life Cycle
  • Java Thread Commands

4
Multitasking
  • Refers to performing several tasks at the same
    time
  • Common multitasking activities
  • Studying while listening to music
  • Doing homework while watching tv
  • Texting while listening to the teacher
  • Doing Java laboratory exercises while chatting
    and looking at Friendster

5
Multitasking
  • Computers normally perform several tasks at the
    same time
  • Each computer user usually opens several windows
    at the same time
  • When an active window finishes processing and
    while the user is still reading the output, the
    computer is expected not to be idle
  • The computer is expected to be processing the
    other non-active windows

6
Multitasking
  • In reality, a person can not do several tasks at
    the same time
  • A person doing multitasking switches among
    several tasks
  • One has to leave behind a current task
    momentarily to attend to a new task

7
Multitasking
  • Similarly, a computer can not do several tasks at
    the same time
  • A computers multitasking feature involves
    switching rapidly among several tasks
  • But the computer performs switching so fast that
    it gives the illusion that it is performing all
    the tasks at the same time

8
Multithreading
  • In a particular program, several tasks can also
    happen at the same time
  • In the same way, the computer switches among
    these tasks rapidly that it appears as if the
    computer is performing them at the same time
  • What are the tasks that are happening at the same
    time in a PacMan program?

9
Multithreading
  • A Thread is a sequential task that happen
    concurrently with other tasks
  • A program can have several threads running at the
    same time
  • This feature is called Multithreading

10
Multithreading
  • A Java program has at least one Thread running
    the Thread that reads, interprets, and runs
    public static void main()
  • Other Threads can be created dynamically to run
    concurrently with this main Thread
  • When all Threads die in a program, the program
    ends

11
Multithreading
  • Threads compete for CPU time according to their
    level of priority
  • The Thread with the currently highest priority is
    set in the foreground and is given a slice of
    time with the CPU
  • The other Threads are set in the background and
    await for their turn when their priority number
    is called

12
Multithreading
  • User Threads
  • Threads that affect how the program runs
  • These threads actively compete for CPU time so
    that they can be executed
  • When all User Threads die, the program ends
  • Daemon Threads
  • Low-priority Threads that perform administrative
    tasks
  • e.g. The Java Garbage Collector

13
Thread Dependence
  • Sometimes, a Thread can depend on the results of
    another Thread to execute
  • For example, several Threads can be used to
    download and play a media clip from the Internet
  • So that the user does not have to wait long, a
    Thread can play the incomplete media clip even
    though the other Thread has not yet finished
    downloading

14
Deadlocks
  • A Thread that depends on the results of another
    Thread sleeps while the other Thread finishes
    execution
  • What happens if Thread1 waits for the results of
    Thread2, while Thread2 waits for the results of
    Thread3, and Thread3 waits for the results of
    Thread1?
  • This situation is called a Deadlock

15
Multithreading Issues
  • Thread issues and solutions will be covered in
    detail in your Operating Systems class in 4th
    Year
  • Managing thread priorities
  • Switching CPU time among threads
  • Detecting and solving deadlocks, etc

16
Java Thread Commands
  • Creating a Thread
  • Either extend class Thread or implement interface
    Runnable
  • e.g.
  • public class Ghost extends Thread
  • public class Ghost implements Runnable

17
Java Thread Commands
  • Creating a Thread
  • Thread classes should overwrite the method run( )
    which is invoked every time the Thread gets CPU
    time
  • View Thread Demo samples

18
Java Thread Commands
  • Methods of class Thread
  • start()
  • Starts the Thread. Calls the run() method of the
    Thread.
  • yield()
  • Allows other Threads of lower priorities to take
    the CPU time.
  • sleep(int milli)
  • Stops the Thread for indicated number of
    milliseconds.

19
Thread Life Cycle
20
Thread Life Cycle
  • Thread started
  • A Thread starts its execution with the call to
    method start(). This calls the Thread run()
    method.
  • myThread.start()
  • Thread runnable
  • A started Thread enters the running phase once
    method start() is invoked. It waits for the CPU
    to give it a slice of time.

21
Thread Life Cycle
  • Thread not runnable
  • A Thread will enter the Not Runnable phase for
    any of the following reasons
  • It has been called to sleep
  • myThread.sleep(1000)
  • It has been called to wait
  • myThread.wait()
  • Waiting Thread can be put in ready state
  • myThread.notify()
  • It issues an I/O request

22
Thread Life Cycle
  • Thread dies
  • A Thread will die after it has completed the task
    in the run() method
  • A dead Thread will no longer be brought back to
    life (resurrected), but the reference can be
    recycled into a new Thread (reincarnation)
  • See Thread demo

23
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com