Professional Documents
Culture Documents
4 3 2 P 603a0f7027700 File
4 3 2 P 603a0f7027700 File
4 3 2 P 603a0f7027700 File
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
Method Meaning
► 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.
12
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");
} }
} }
} }
} }