Sie sind auf Seite 1von 20

CT038-3-2

Object Oriented Development with


Java

Multi threading
Topic & Structure of the lesson

Thread
Threads methods
Thread Life Cycle
Runnable Interface

CT038-3-2 OODJ Multi threading


Learning outcomes
At the end of this lecture you should be
able to:
To understand how multiple threads can
execute in parallel
To learn how to implement threads

CT038-3-2 OODJ Multi threading


Key terms you must be able to use

If you have mastered this topic, you should


be able to use the following terms correctly
in your assignments and exams:
Multi threading

CT038-3-2 OODJ Multi threading


Threads

A thread is a program unit that is executed


independently of other parts of the
program
The Java Virtual Machine executes each
thread in the program for a short amount
of time
This gives the impression of parallel
execution

CT038-3-2 OODJ Multi threading


Creating Threads

Threads are implemented in the form of


objects that contain a method called run( ).
public void run( )
{

}

CT038-3-2 OODJ Multi threading


The run ( ) should be invoked by an
object of the concerned thread.
This can be done by another thread
method called start ( ).
A new thread can be created in two
ways.
1. By creating a thread class
2. By converting a class to a thread.
CT038-3-2 OODJ Multi threading
Creating a thread class

Define a class that extends Thread class


and override its run ( ) method.
The run ( ) is overridden with the code
required by the thread.

CT038-3-2 OODJ Multi threading


Converting a class to a thread

Define a class that implements Runnable


interface.
The Runnable interface has only one
method, run ( ).
This method is to be defined in the method
with the code to be executed by the
thread.

CT038-3-2 OODJ Multi threading


Extending a thread class

We can make our class runnable as a


thread by extending the class
java.lang.Thread.
This gives access to all thread methods
directly.

CT038-3-2 OODJ Multi threading


This includes the following steps:
1. Declare the class extending Thread
class.
2. Implement run ( ).
3. Create a thread object and call start ( ).

CT038-3-2 OODJ Multi threading


Declaring the class

The Thread class can be extended as


follows:
class MyThread extends Thread
{

}

CT038-3-2 OODJ Multi threading


Implement the run ( ) method
The run ( ) has been inherited by the class
MyThread.
We have to override this method to
implement the code to be executed by
thread.
public void run ( )
{
.
}

CT038-3-2 OODJ Multi threading


Starting new thread

To create and run an instance of our


thread class:
MyThread aThread = new MyThread ( );
aThread.start ( );

OR

new MyThread ( ).start( );

CT038-3-2 OODJ Multi threading


Stopping a thread

aThread.stop ( );
This causes the thread to move to the
dead state.

CT038-3-2 OODJ Multi threading


Blocking a thread

A thread can be temporarily suspended or


blocked.
sleep ( ) // blocked for a specified time
suspend ( ) // blocked until further orders
wait ( ) //blocked until certain conditions
occurs
resume ( )
notify ( )

CT038-3-2 OODJ Multi threading


Thread Life Cycle

The states a thread may go through are:


1. Newborn
2. Runnable
3. Running
4. Blocked
5. Dead

CT038-3-2 OODJ Multi threading


Thread Synchronization

A lock object is used to control threads


that manipulate shared resources

synchronized void update ( )


{

}

CT038-3-2 OODJ Multi threading


Implementing the Runnable Interface

Place the code for your task into the run


method of your class
public class MyRunnable implements Runnable
{
public void run()
{
// Task statements go here
. . .
}
}

CT038-3-2 OODJ Multi threading


Create an object of your subclass
Runnable r = new MyRunnable();
Construct a Thread object from the
runnable object.
Thread t = new Thread(r);
Call the start method to start the thread.
t.start();

CT038-3-2 OODJ Multi threading