Extends Thread Class: Thread Programming

In this blog, we will see child process B and main process Thread4. Process B extends Thread class. Thread4 is main process controls the child process B.

Work of both Processes

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

Main Process Thread4: Display 1 to 10 20 seconds.

 

Location:

Child Process B: Store in package classStore.

Main Process Thread4: Store in package examples.

 

Extends Thread Class:

Child process B extends with Thread Class. Using of this extension child process run in run() method.

 

 Child Process B

package classStore;

public class B extends Thread

{

            public B()

            {

                        start();  // start to thread

            }

            public void run()

            {

                        // 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 Thread4 

package examples;

import classStore.*;

public class Thread4

 {

                public static void main(String[] args)

                {

                                B  obj = new B();               // 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

414total visits,6visits 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