Advanced Java Programming - Old Questions

Question Answer Details

5. Discuss the use of multithreading with suitable example. (5)

5 marks
Asked in 2069

Answer

AI Generated Answer

AI is thinking...

Official Answer

The process of executing multiple threads simultaneously is known as multithreading. The main purpose of multithreading is to provide simultaneous execution of two or more parts of a program to maximum utilize the CPU time. A multithreaded program contains two or more parts that can run concurrently. Each such part of a program called thread.

Example:

Program to create two threads. The first thread should print numbers from 1 to 10 at intervals of 0.5 second and the second thread should print numbers from 11 to 20 at the interval of 1 second.

class First extends Thread

{

    @Override

    public void run()

    {

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

        {

            System.out.println(i);

            try

            {

                Thread.sleep(500);

            }

            catch (InterruptedException e)

            {

                System.out.println(e.getMessage());

            }

        }

 

    }

}

class Second extends Thread

{

    @Override

    public void run()

    {

        for (int i=11; i<=20; i++)

        {

            System.out.println(i);

            try{

                Thread.sleep(1000);

            }

            catch (InterruptedException e)

            {

                System.out.println(e.getMessage());

 

            }

        }

    }

}

public class ThreadInterval

{

    public static void main(String[] args)

    {

        Thread first = new First();

        Thread second= new Second();

        first.start();

        second.start();

    }

}