Professional Documents
Culture Documents
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
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
..
.. ..
..
}
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.
obj.stop();
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.
start()
stop() run() yield() suspend() resume()
sleep(t)
wait() notify()
New Thread
Runnable
Not runnable
States of Threads
1. Newborn state
2. Runnable state 3. Running state 4. Blocked state 5. Dead state.
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.
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