Programming Language JAVA Unit 6 12 Basics of

Programming Language (JAVA) Unit 6. 12 - Basics of Threading Presentation 1

Revision 1. Define an applet and explain its lifecycle. 2. State the differences between applet and application? 3. Describe the use of graphic class.

Objectives At the end of this presentation, you will be able to • Explain the need of multitasking • Recognize the concurrent execution of multithreaded applications • Describe the uses of threads

Thread – Definition Thread can be defined as single, sequential flow of control within a program.

Thread – Types Are of two types • Single thread • Multiple threads

Single threaded Application • A task or process that is made up of only one thread is single-thread. • A program that supports single thread is called single threaded application.

Multi-threaded Application • A task or process that is made of more than one thread is multiple threading. • A program that supports more than one thread is multi-threaded application

Multi-tasking • Is the ability of an operating system to execute more than one program simultaneously.

Difference Between Multitasking and Multithreading S. No: Multitasking Multithreading 1. More than one program gets executed simultaneously. More than one part of a program called threads is executed simultaneously. 2. Multitasking is a timesharing process. CPU switches from one program to another program so quickly to complete all the programs simultaneously. Multithreading is also a timesharing process. CPU switches from one activity to another activity within the program so quickly to complete all the activities simultaneously. 3. Since each program occupies different memory location, Multitasking is called as heavyweight process Multithreading is a light weight process because the activities/ threads share the same memory space.

Uses of Threads • A program can be split into separate tasks and further into threads. This increases the effective usage of CPU during execution. • The popularity of threading increased with Graphical User Interfaces. Threading system allowed the user to attain better performance in a program. • The introduction of threads created an illusion that the program's performance was faster.

Importance of Threads • In a Java program, one thread need not wait for another thread to complete. This is called asynchronous behaviour of threads. • Java provides few techniques to handle the asynchronous behaviour of threads. • Minimise the CPU's idle time.

Summary In this presentation, you learnt the following • Threads can be defined as single, sequential flow of control within a program. • The two types of threads are single and multiple threads • A program that supports single thread is single threaded application. • A program that supports more than one thread is multi-threaded application. • Multi-Tasking is the ability of an operating system to execute more than one program simultaneously.

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:

Hands-On!

Output

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 ………………. . Thread 1 Switching ………………. . Thread 2

Declaring a Thread class - Syntax class <new thread> extends Thread { ……………. . }

Declaring a Thread Class - Example class Thread_2 extends Thread { ……………. . }

Implementing the run() method public void run() { …………… …………… }

Starting a new Thread - Syntax Threadname objectname = new Threadname; objectname. start();

Starting new Thread - Example Thread_2 obj = new Thread_2(); obj. start();

Hands-On! • The program Thread. Demo. java illustrates the creation of multithreaded application

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

Hands-On! • The Thread. Demo 2. java program is a multithreaded application that creates two threads

Coding

output

Activity 6. 12. 2 Step 1: Open Thread. Demo 2. 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 Thread. Demo 2. 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 - Syntax objectname. stop();

Stopping a Thread - Example obj. 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(berlalu). suspend() - The thread is suspended(digantung) 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() Used to initiate thread. stop() Used to kill the thread. run() Used to implement the body of the thread and its behaviour. yield() Used to give the control to another thread of equal priority. suspend() Used to suspend the thread. resume() Used to resume the suspended thread.

Methods in Thread Class –Contd… sleep(t) Used to specify the time frame for suspension. wait() Used to make thread wait until some event occurs. notify() Used to schedule 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 Description Newborn When a thread is created. Runnable It is ready for execution and is waiting for the availability of CPU. Running It is the time given for the execution of the thread. Blocked The thread is suspended, sleeping or waiting to fulfill certain requirements. Dead A thread completes executing the run() method, ends its life.

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(dihidupkan semula) 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 Thread. Demo 3. java program illustrates the life cycle of a thread.

Activity 6. 12. 3 Step 1: Open Thread. Demo 3. 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.

coding

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.
- Slides: 53