Monday, 7 September 2015

Multithreading in JAVA using Runnable Interface

                       After learning How to perform Multithreading in Java, the next step is to learn the same by using Runnable Interface. As explained in earlier post (Multithreading in Java) a class needs to extend Thread Class to achieve Multithreading. But when we want to do the same by using Runnable Interface, we must understand that Runnable is a interface not a class.

                        Remember that a Thread is a class & it is responsible for managing CPU/Processor resources. On the other hand Runnable is an interface containing only abstract methods with no implementations. The only benefit of implementing Runnable Interface is that, the object which implements this interface becomes eligible to be used with a constructor of a Thread Class for creating new Threads. Also we can set priority to individual thread by using setPriority() method or relative priority by getting priority of one thread by using getPriority() method & setting for another thread. Generally range of priority is on the scale of 1 to 10. 1 is minimum & 10 is maximum. main() method has priority as 5.

Following is the sample code of the implementation. You can also get it from my GitHub repository.

/**
 *
 * @author YogeshD
 */
class One implements Runnable
{
    public void run()
    {
        System.out.print("You are inside of Class One:");
        for(int i=1;i<=5;i++)
        {
            System.out.println("i="+i);
        }
        System.out.println("End of Class One");
    }
}
class Two implements Runnable
{
    public void run()
    {
        System.out.print("You are inside of Class Two:");
        for(int j=1;j<=5;j++)
        {
            System.out.println("j="+j);
        }
        System.out.println("End of Class Two");
    }
}
class Three implements Runnable
{
    public void run()
    {
        System.out.print("You are inside of Class Three:");
        for(int k=1;k<=5;k++)
        {
            System.out.println("k="+k);
        }
        System.out.println("End of Class Three");
    }
}
public class Multithreading_Java_Runnable {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        One obj_One=new One(); //Object of class
        Two obj_Two=new Two();
        Three obj_Three=new Three();
           
        Thread One_Thread=new Thread(obj_One);//Onject is used with the constructor
        Thread Two_Thread=new Thread(obj_Two);
        Thread Three_Thread=new Thread(obj_Three);
        
        //Set priority to a perticular Thread. 
        //Priority is on the scale of 1-10. (Lowest to Highest)
        Three_Thread.setPriority(8);
        //One_Thread.setPriority(Three_Thread.getPriority()-3);//Relative way to set priority
        Two_Thread.setPriority(Thread.MIN_PRIORITY);
        
                
        //Start Thread Life cycle
        One_Thread.start();
        Two_Thread.start();
        Three_Thread.start();
    }
}
Please note that output of multithreading programs may vary as per the machine, as it depends on the CPU/Processor.
Sample Output Executed First Time:


run:
You are inside of Class One:You are inside of Class Three:k=1
k=2
k=3
k=4
k=5
End of Class Three
i=1
i=2
i=3
i=4
i=5
End of Class One
You are inside of Class Two:j=1
j=2
j=3
j=4
j=5
End of Class Two
BUILD SUCCESSFUL (total time: 0 seconds)
Minimum priority has been set to Thread Two. Sample Output Executed Second Time:


run:
You are inside of Class One:i=1
i=2
i=3
i=4
i=5
End of Class One
You are inside of Class Three:You are inside of Class Two:k=1
k=2
k=3
k=4
k=5
End of Class Three
j=1
j=2
j=3
j=4
j=5
End of Class Two
BUILD SUCCESSFUL (total time: 0 seconds)

==>Posted By Yogesh B. Desai

Next Post: Bouncing Ball Program in JAVA using Multithreading and Applet

Previous Post: Multithreading In JAVA

You are Visitor Number:
conter12

No comments:

Post a comment