Multithreading Process in Java Program

What is multithreading process?

Multithreading Process in java program means multiple child process working together and control by main process.

In this blog, we will see how three child processes Process-1, Process-2 and Process-3 work together.

Work of three child processes are listed below.

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

Child Process-2: Display 1 to 15 in 15 seconds.

Child Process-3: Display 1 to   5 in   5 seconds.

 

Let’s see example on Multithreading Process

Child class in classStore 

package classStore;

public class Child implements Runnable

{

            Thread t;          // Ref

            String processName;

            public Child(String processName)

            {

                        t = new Thread(this);   // intilization

                        this.processName = processName;

                        t.start();                                                // start thread process

            }

            // Child – 1: display 1 to 10

            // Child – 2: display 1 to 15

            // Child – 3: display 1 to 5

 

            public void run()

            {

                        try

                        {

                                    if(processName.compareTo(“Child-1”) == 0)

                                    {

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

                                                {

                                                            Thread.sleep(1000);

                                                            System.out.println(processName +” : ” + i);

                                                }

                                    }

                                    else if(processName.compareTo(“Child-2”) == 0)

                                    {

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

                                                {

                                                            Thread.sleep(1000);

                                                            System.out.println(processName +” : ” + i);

                                                }

                                    }

                                    else if(processName.compareTo(“Child-3”) == 0)

                                    {

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

                                                {

                                                            Thread.sleep(1000);

                                                            System.out.println(processName +” : ” + i);

                                                }

                                    }

                        }

                        catch(Exception ex){}  

            }

}


Thread5 in examples package

package examples;

import classStore.*;

public class Thread5

{

                public static void main(String[] args)

                {

                                Child obj1 = new Child(“Child-1”);

                                Child obj2 = new Child(“Child-2”);

                                Child obj3 = new Child(“Child-3”);

                }

}

Output:

Child Process-1 display 1 to 10 in 10 seconds

Child Process-2 display 1 to 15 in 15 seconds

Child Process-3 display 1 to 5 in 5 seconds


View Output in video.


Earn Money Online

748total visits,4visits today


mayank

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

Related articles

Comments are closed.