Threads in Java

前端 未结 9 885
野趣味
野趣味 2020-11-30 01:11

I was today asked in an interview over the Thread concepts in Java? The Questions were...

  1. What is a thread?
  2. Why do we go for threading?
  3. A re
9条回答
  •  佛祖请我去吃肉
    2020-11-30 02:03

    Multithreading is a Java feature that allows concurrent execution of two or more parts of a program for maximum utilisation of CPU. Each part of such program is called a thread. So,

    Threads are light-weight processes within a process.

    Threads can be created by using two mechanisms :

    1. Extending the Thread class
    2. Implementing the Runnable Interface

    Thread creation by extending the Thread class

    We create a class that extends the java.lang.Thread class. This class overrides the run() method available in the Thread class. A thread begins its life inside run() method. We create an object of our new class and call start() method to start the execution of a thread. Start() invokes the run() method on the Thread object.

    class MultithreadingDemo extends Thread{
    public void run()    {
        try {   // Displaying the thread that is running
            System.out.println ("Thread " + Thread.currentThread().getId() 
                                    + " is running"); 
            }
            catch (Exception e){   // Throwing an exception
                System.out.println ("Exception is caught");
            }
        }
    } 
    public class Multithread{
        public static void main(String[] args)    {
            int n = 8; // Number of threads
            for (int i=0; i<8; i++)        {
                MultithreadingDemo object = new MultithreadingDemo();
                object.start();
            }
        }
    }
    

    Thread creation by implementing the Runnable Interface

    We create a new class which implements java.lang.Runnable interface and override run() method. Then we instantiate a Thread object and call start() method on this object.

    class MultithreadingDemo implements Runnable{
    public void run()    {
        try   {     // Displaying the thread that is running
            System.out.println ("Thread " +  Thread.currentThread().getId() +
                                " is running");
    
        }
        catch (Exception e)   {     // Throwing an exception
            System.out.println ("Exception is caught");
        }
        }
    } 
    class Multithread{
        public static void main(String[] args)    {
            int n = 8; // Number of threads
            for (int i=0; i<8; i++)        {
                Thread object = new Thread(new MultithreadingDemo());
                object.start();
            }
        }
    }
    

    Thread Class vs Runnable Interface

    1. If we extend the Thread class, our class cannot extend any other class because Java doesn’t support multiple inheritance. But, if we implement the Runnable interface, our class can still extend other base classes.

    2. We can achieve basic functionality of a thread by extending Thread class because it provides some inbuilt methods like yield(), interrupt() etc. that are not available in Runnable interface.

提交回复
热议问题