You are on page 1of 36

Programming Language (JAVA)

Unit 6.12 - Basics of Threading

Presentation 2

Objectives
At the end of this presentation, you will be able to Create multithreaded application List the methods involved in life cycle of a thread

Single Threaded Program - Example


A process that is made of only one thread is called single threaded application. class Thread_1 { public static void main(String args[]) { } }

Hands-On!
The program Thread_1.java displays the text, Single Threaded in a single threaded application:

Creating Threads
In a single threaded application, main() itself acts as a thread. If a program requires more than one thread, it needs to be created
.. .. .. Main Thread

.. .. ..

Switching

.. .. ..

Thread 1

Thread 2

Declaring a Thread Class - Example


class Thread_2 extends Thread
{ ..

..
.. ..

..
}

Declaring a Thread class - Syntax


class <new thread> extends Thread
{ .. .. .. .. .. }

Implementing the run() method


public void run()
{

Starting new Thread - Example

Thread_2 obj = new Thread_2(); obj.start();

Starting a new Thread - Syntax


Threadname objectname = new Threadname;
objectname.start();

Hands-On!
The program ThreadDemo.java illustrates the creation of multithreaded application

Activity 6.12.1
Step 1: Open ThreadDemo.java data file. Step 2: Type class Thread_2 extends Thread in code line 3. Step 3: Save the program as ThreadDemo.java Step 4: Compile the program. Step 5: Execute the program.

Hands-On!
The ThreadDemo2.java program is a multithreaded application that creates two threads

Activity 6.12.2
Step 1: Open ThreadDemo2.java from Student data file. Step 2: Edit the code line 18 as int c = 3, d = 4, e = 5, product;. Step 3: Edit the code line 19 as product = c * d * e; Step 4: Edit the code line 21 as System.out.println(("Product is "+product); Step 5: Save the program as ThreadDemo2.java. Step 6: Compile the program. Step 7: Execute the program.

Stopping and Blocking Threads


A thread during the running state can be moved to the not runnable state. This can be achieved by:
Stopping a thread Blocking a thread

Stopping a Thread - Example

obj.stop();

Stopping a Thread - Syntax

objectname.stop();

Blocking a Thread
A thread can be temporarily suspended or blocked from running. The thread is blocked from entering the runnable or running state. A thread can be blocked using any of the three methods. sleep() - The thread is blocked for a specified time. The thread returns to the runnable state when the specified time is elapsed.
suspend() - The thread is suspended until further ordered. The resume() method is called. wait() - The thread is blocked until a condition occurs. The notify() method is called.

Methods in Thread Class


Methods Description

start()
stop() run() yield() suspend() resume()

Used to initiate the thread.


Used to kill the thread. Used to implement the body of the thread and its behaviour. Used to give the control to another thread of equal priority. Used to suspend the thread. Used to resume the suspended thread.

Methods in Thread Class Contd

sleep(t)
wait() notify()

Used to specify the time frame for suspension.


Used to make the thread wait until some event occurs. Used to schedule the thread to run again.

Life Cycle of a Thread

New Thread

Runnable

Not runnable

run() method terminates.


Dead

States of Threads
1. Newborn state
2. Runnable state 3. Running state 4. Blocked state 5. Dead state.

States in a Life Cycle of a Thread


States Newborn Runnable Description When a thread is created. It is ready for execution and is waiting for the availability of CPU. It is the time given for the execution of the thread. The thread is suspended, sleeping or waiting to fulfill certain requirements. A thread completes executing the run() method, ends its life.

Running Blocked

Dead

Newborn State
A thread object when created is said to be in newborn state.
In this state, the thread can be schedule for running state either using start() method or kill it using stop() method.

Runnable State
A thread when ready for execution and is waiting for the availability of CPU said to be in runnable state.
The thread in this state joins the queue of threads that are waiting for execution.

The threads in the queue are executed based on the given priority.
The thread gives up the control to another thread of equal priority using the yield() method.

Running State
A thread when given time for its execution is said to be in running state.
The thread runs until it loses the control on its own or a higher priority thread prevents it.

suspend() method
The thread is suspended using suspend() method. A suspended thread can be revived using the resume() method.
This is possible when a thread is required to be suspended for sometime but is not killed.

sleep() method
The thread is suspended using sleep(t) method.
A suspended thread can be revived using the resume() method.

This is possible when a thread is required to be suspended for sometime but is not killed.

wait() method
The thread is made to wait until some event occurs. This is done using wait()
The thread is scheduled to run again using notify()

Blocked State
When the thread is suspended, sleeping or waiting to fulfil certain requirements, the thread is said to be in a blocked state.
The blocked thread is neither in a runnable state nor in a dead state.

Dead State
A thread that has completed the run() method, ends its life.
The thread is in the dead state. When a thread is stopped implementing the stop() method it has reached the dead state.

Hands-On!
The ThreadDemo3.java program illustrates the life cycle of a thread.

Activity 6.12.3
Step 1: Open ThreadDemo3.java data file.
Step 2: Identify the methods that are involved in the life cycle of a thread.

Step 3: List the methods involved in the life cycle of a thread.

Lab Exercise
1. Write a program that has two threads. One thread should calculate the square of a number while the other thread should calculate the cube of another number. Display the square and cube of the numbers.

Summary
In this presentation, you learnt the following A process that is made of only one thread is called single threaded application. A single threaded application can perform only one task at a time. In a single threaded application, main() itself acts as a thread. If a program requires more than one thread, it needs to be created. The lifetime of a thread includes the newborn, runnable, running, blocked and dead states.

Assignment

1. Explain the Life cycle of a thread?


2. Write a program that has two threads. One thread should calculate the area of the circle; the second thread should calculate the area of a square. Display the results. List the methods of the Thread class in this program.

You might also like