Implements Runnable Interface: Thread Programming

In this blog, we will see child process A and main process Thread3. Process A implements runnable interface. Thread3 is main process controls the child process A.

Work of both Processes

Child Process A: Display 1 to 10 in 10 seconds.

Main Process Thread3: Display 1 to 10 20 seconds.

 

Location:

Child Process A: Store in package classStore.

Main Process Thread3: Store in package examples.

 

Implements Runnable Interface:

Child process A implements with runnable interface. Using of this implementation child process run in run() method.

 

Example on Child And Main Process with Runnable Interface 

Child Process

package classStore;

public class A implements Runnable

{

            Thread t;          // ref of thread class

            public A()

            {

                        t = new Thread(this);   // create an object using this ref | initialization

                        t.start();                        // start to thread process         | start

            }

            public void run()                       // | running mode

            {

                        // Process – two display 1 to 10 in every 1 sec

                        try

                        {

                                    for(int i=1;i<=10;i++)

                                    {

                                                Thread.sleep(1000);

                                                System.out.println(“Child = ” + i);

                                    }

                        }

                        catch(Exception ex){}

            }

}

 


Main Process

package examples;

import classStore.*;

public class Thread3

 {

                public static void main(String[] args)

                {

                                A obj = new A();               // call to child process

 

                                try

                                {

                                                for(int i=1;i<=10;i++)

                                                {

                                                                Thread.sleep(2000);

                                                                System.out.println(“Main = ” + i);

                                                }

                                }

                                catch(Exception ex){}    

                }

}


Output:

Child Process display 1 to 10 in 10 seconds

Main Process display 1 to 10 in 20 seconds

View Output in video.

 

Video also available on YouTube Java Channel


 

Earn Money Online

Share and Enjoy

  • Facebook
  • Twitter
  • Delicious
  • LinkedIn
  • StumbleUpon
  • Add to favorites
  • Email
  • RSS

329total visits,4visits today


mayank

Hello, I am Maddy a Java blogger, trainer and developer

Related articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Your comments (*)

Name (*)

Your full name please.

Email address (*)

Used for gravatar.

Website

Link back if you want.

Enter Captcha Here :*

Reload Image

EmailEmail
PrintPrint