In this article,Runnable Interface in Multi-Threading is explained in detail with examples.
We perform Multi-threading using "Thread class" in basic 4 Steps.In situations,when we already have the inheritance in our program.(From topic Runnable Interface in Multi-Threading)
We perform Multi-threading using "Thread class" in basic 4 Steps.In situations,when we already have the inheritance in our program.(From topic Runnable Interface in Multi-Threading)
Ex
A->B
class B extends A
{---
----
}
Now to perform Multi-Threading in class B(Subclass) using Thread class is not possible because java doesn't support Multiple Inheritance of two base classes.
Ex
class B extends A extends thread //not possible in java.
{
-----
---
}
However still to perform multithreading subclass B java provides an alternate solution i.e,"Runnable interface/Runnable Interface in Multi-Threading".
Runnable interface is a base class of thread class,which is available in "Java.lang" package.
We use runnable interface to implement multithread in our subclass B.
It is done as follows
Steps
1)Inherit your Sub-Class from Runnable interface
Ex
class B extends A implements Runnable
{-----}
2)Runnable interface contains only one abstract method just redefine that method in your subclass B
Ex
class B extends A implements Runnable
{
public void Run()
{
---
---
}
}
3)Create an object of your subclass B
B t=new B();
4)The above 't' is not a full fledged thread object hence we create an object of thread class and pass our object 't' as a parameter to thread class constructor.
Ex Thread tt=new Thread(t);
5) Now call/invoke the run() of your class by using start() of Thread-class.
Ex tt.start();
Note
public static native Thread Current Thread();
This method is available in thread class, it is used in Run Method,it returns object reference of that thread object which is currently in running state (taking CPU time)
Ex
//program related to Runnable Interface in Multi-Threading..
class ExampleThread4 implements Runnable
{
public void run()
{
while(true) // as the loop is true the output prints continuosly.to stop it use CTRL+C.
{
System.out.println(Thread.currentThread().getName());
}
}
}
public class Thread4
{
public static void main(String args[])
{
ExampleThread4 t=new ExampleThread4();
Thread t1=new Thread(t,"AAAA");
Thread t2=new Thread(t,"BBBB");
t1.start();
t2.start();
}
}
{
public void run()
{
while(true) // as the loop is true the output prints continuosly.to stop it use CTRL+C.
{
System.out.println(Thread.currentThread().getName());
}
}
}
public class Thread4
{
public static void main(String args[])
{
ExampleThread4 t=new ExampleThread4();
Thread t1=new Thread(t,"AAAA");
Thread t2=new Thread(t,"BBBB");
t1.start();
t2.start();
}
}
Output
Continue to next topic IO streams in Java.
Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing
Do check my new startup Surprise Planners in Hyderabad- Lavish Surprises our services are surprise party planners in Hyderabad, surprise gifts, surprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.
Hi Friends, Please comment down your views in the comment section below. Thank you...
No comments:
Post a Comment