4 3 2 P 603a0f7027700 File

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

CO_IF_22412_UO3

Sushama Pawar, Lecturer, Vidyalankar Polytechnic

Date: 02 February 2021

MSBTE LEAD: Learning at your Doorstep


Unit 04 :
Exception Handling and
Multithreading

Written by

Sushama Pawar
Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
Unit Outcome 3 : Create thread to
run the given multiple processes in
the given program.

Written by

Sushama Pawar
Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
Learning Outcome 3b : Students
should create thread by extending to
thread class and by implementing
runnable interface.

Written by

Sushama Pawar
Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai
What we will learn today

1.
1. Create thread by extending thread class. Key takeaways
2.
2. Example Create thread by extending thread class.

Sushama Pawar
Lecturer, Department of Information Technology[NBA Accredited],Vidyalankar Polytechnic,
Mumbai

Page 5 Maharashtra State Board of Technical Education 2 Feb 2021


Concept Map

Create thread by extending


thread class.

Create thread by extending


runnable interface

Difference between extending


thread and implementing
runnable interface

Page 6 Maharashtra State Board of Technical Education 2 Feb 2021


Learning Objective/ Key learning

► Understand how to create thread in java.


► Example

Page 7 Maharashtra State Board of Technical Education 2 Feb 2021


Thread Methods

Method Meaning

getName( ) Obtains a thread’s name.

getPriority( ) Obtains a thread’s priority

isAlive( ) Determines whether a thread is still


running
join( ) Waits for a thread to terminate

run( ) Creates entry point for the thread

sleep( ) Suspends a thread for some period of


Time 8
start( ) Starts a thread by calling its run
Method

Page 8 Maharashtra State Board of Technical Education 2 Feb 2021


Creating a thread by implementing the Runnable interface

► To implement Runnable, a class need only implement a single method called run( ), which is declared
in Runnable as:
public void run( )
► The method run( ) can call other methods.
► After creating a class that implements Runnable, we have to instantiate an object of type Thread
from within that class. Thread defines several constructors. The general form is:
► start() method of Thread class is used to start a newly created thread. It performs following tasks:
o A new thread starts(with new callstack).
o The thread moves from New state to the Runnable state.
o When the thread gets a chance to execute, its target run() method will run.

Page 9 Maharashtra State Board of Technical Education 2 Feb 2021


Example of Creating the thread using Runnable interface

class NewThread implements Runnable System.out.println("Exiting child Output:


{ thread.");
Thread t; }
NewThread() //statement3 }
{ class ThreadDemo
t = new Thread(this, "Demo Thread"); {
System.out.println("Child thread: " + t); public static void main(String args[])
t.start(); // Statement4 throws InterruptedException
} {
public void run() new NewThread(); // statement1
{ for(int i = 5; i > 0; i--)
for(int i = 5; i > 0; i--) {
{ System.out.println("Main Thread: " +
System.out.println("Child Thread: " + i); i);
try { Thread.sleep(1000); //statement2
Thread.sleep(500); //statement5 }
} catch(Exception e){ } System.out.println("Main thread
} exiting.");
}
}
Page 10 Maharashtra State Board of Technical Education 2 Feb 2021
Example: Write a thread program for implementing the “Runnable interface‟ Or
program to print even numbers from 1 to 20 using Runnable Interface

class mythread implements Runnable


{
public void run()
{
System.out.println("Even numbers from 1 to 20 : ");
for(int i= 1 ; i<=20; i++)
{
if(i%2==0) System.out.print(i+ " ");
}
}
}
class test
{
public static void main(String args[])
{
mythread mt = new mythread();
Thread t1 = new Thread(mt);
t1.start();
}
}

Page 11 Maharashtra State Board of Technical Education 2 Feb 2021


Concept Explanation: Creating a thread in Java

There are two ways to create a thread in Java:


1) By extending Thread class.
2) By implementing Runnable interface.

12

Page 12 Maharashtra State Board of Technical Education 2 Feb 2021


Creating a thread by extending thread class

► By extending thread class


• User specified thread class is created by extending the class ‘Thread’.
• For creating a thread a class has to extends the thread class that is java.lang.Thread
• The class should override the run() method.
• The functionality that is expected by the Thread to be executed is written in the run() method.
Syntax: -
Declare class as extending thread
class Mythread extends Thread
{
____
------
}

Page 13 Maharashtra State Board of Technical Education 2 Feb 2021


Example of Creating the thread using Extending thread-Develop a program to create two threads
such that one thread will print odd no. and another thread will print even no. between 1-20

class odd extends Thread class even extends Thread class oddeven2 Output:
{ { {
public void run() public void run() public static void main(String arg[])
{ { {
for(int i=1;i<=20;i=i+2) for(int i=0;i<=20;i=i+2) odd o=new odd();
{ { even e=new even();
System.out.println("ODD="+i); System.out.println("EVEN="+i); o.start();
try try e.start();
{ { }
sleep(1000); sleep(1000); }
} }
catch(Exception e) catch(Exception e)
{ {
System.out.println("Error"); System.out.println("Error");
} }
} }
} }
} }

Page 14 Maharashtra State Board of Technical Education 2 Feb 2021


Difference between Creating thread by extending thread class and runnable interface.

Page 15 Maharashtra State Board of Technical Education 2 Feb 2021


Thank You

You might also like