Presentation is loading. Please wait.

Presentation is loading. Please wait.

21 Threads.

Similar presentations


Presentation on theme: "21 Threads."— Presentation transcript:

1 21 Threads

2 Previously Properties of OO Programming Encapsulation Classes
The Taxonomy of Insects Hierarchical Taxonomy Objects – Data Structures that Inherit Multiple Inheritance Polymorphism Dynamic Method Binding

3 Overview Concurrency Multiple processes vs Threads Why Threads?
Class vs Interface Thread life cycle

4 Concurrency "a property of systems in which several computations are executing simultaneously, and potentially interacting with each other" E.g. a 2-player computer fighting game - both characters movements and actions are independent of the other

5 Why Threads? To perform a time-consuming task without locking up the event-dispatching thread. making extensive calculations, doing something that results in many classes being loaded (initialization, for example) blocking for network or disk I/O. To perform an operation repeatedly, usually with some predetermined period of time between operations. To wait for messages from clients.

6 Multiple processes vs Threads
Threads are lightweight compared to processes Threads share the same address space and therefore can share both data and code Context switching between threads is usually less expensive than between processes Cost of thread intercommunication is relatively low that of process intercommunication Threads allow different tasks to be performed concurrently.

7 Class or Interface? Two ways to create a thread in Java:
Extend the Thread class Implement the Thread interface Remember there is no multiple inheritance in Java. So ONLY extend the Thread class if no other classes are being extended Otherwise implement the Thread Interface Threads are part of java.lang so no need to import anything- they're always there!!!

8 Thread Class A class extending the Thread class overrides the run() method from the Thread class to define the code executed by the thread This subclass may call a Thread constructor explicitly in its constructors to initialize the thread, using the super() call The start() method inherited from the Thread class is invoked on the object of the class to make the thread eligible for running

9 Thread Interface A class implements the Runnable interface, providing the run() method that will be executed by the thread. An object of this class is a Runnable object An object of Thread class is created by passing a Runnable object as argument to the Thread constructor. The Thread object now has a Runnable object that implements the run() method The start() method is invoked on the Thread object created in the previous step. The start() method returns immediately after a thread has been spawned The thread ends when the run() method ends, either by normal completion or by throwing an uncaught exception

10 Waiting/Blocking/Sleeping
Thread life cycle JVM setPriority(int iPriorty) iPrioririty with values 1 (MIN_PRIORITY) to 10 (MAX_PRIORITY) Exiting the run method moves the thread to the “Dead” state new Thread() Threadi New notify() notifyAll() start() Runnable Waiting/Blocking/Sleeping Scheduler yield() Scheduler Running Alive join() wait() sleep() Syncronized ... Dead Dead threads cannot be brought back to life call to start() on a dead thread will get a runtime exception


Download ppt "21 Threads."

Similar presentations


Ads by Google