8 Dec 2017

What is multithreading and how many way we can create Thread ? explain each of them.

Multithreading is a process of creating multiple threads in java stack area (JSA) for executing multiple task concurrently to complete their execution in less time by using processor ideal time effectively.
now what is process? if we talk about process we can say that program under execution is called process. and lightweight process is called thread . in other way we can say ,Thread is independent sequential flow of execution.

Multithreading is also lightweight because of  the contexts switching is very fast and effective among threads.and switching is very fast because of thread stored in same address.
When JVM is created in its JSA area by default two threads are created these are following….

  1. Main thread-to execute java methods
  2. Garbage collector-to destroy unreferenced objects
We can create thread by 4 ways, these are following
  1. By extending Thread class
  2. By implementing Runnable interface
  3. By implementing Callable interface 
  4. By anonymous class (Inline Thread)

By extending Thread class

class ThreadUsingThread extends Thread
{
          public void run()
          {
                   System.out.println("I am form run");
          }
          public static void main(String[] args)
          {
                   ThreadUsingThread t = new ThreadUsingThread();
                   t.start();
          }
}

Output

By implementing Runnable interface

We can define a Thread even by implementing Runnable interface also.Runnable interface present in java.lang.pkg and contains only one method run().

class ThreadUsingRunnable implements Runnable
{
          public void run()
          {
                   System.out.println("I am form run");
          }
          public static void main(String[] args)
          {
                   ThreadUsingThread tut = new ThreadUsingThread();
                   Thread t=new Thread(tut);
                   t.start();
          }
}

Output


By implementing Callable interface

  1. In the Case of Runnable Job Thread won’t Return anything.
  2. If a Thread is required to Return Some Result after Execution then we should go for Callable.
  3. Callable Interface contains Only One Method public Object call() throws Exception.
  4. If we Submit a Callable Object to Executor then the Framework Returns an Object of Type java.util.concurrent.Future
  5. The Future Object can be Used to Retrieve the Result from Callable Job.

import java.util.concurrent.*;
class ThreadUsingCallable implements Callable
{
          int num=10;
          public Object call() throws Exception
          {
                   return num;
          }
          public static void main(String[] args) throws Exception
          {
                   ThreadUsingCallable tuc = new ThreadUsingCallable();
                   ExecutorService service=Executors.newFixedThreadPool(3);
                   Future f=service.submit(tuc);
                   System.out.println(f.get());
                   service.shutdown();
          }
}

Output




By anonymous class (Inline Thread)

Actually there are two way to create inline Thread,these are-
  1. Create anonymous class with Thread object 
  2. Create anonymous class with Runnable object
Using Thread object

class ThreadUsingAnonymous
{
          public static void main(String[] args)
          {
                   Thread thread = new Thread() {
                    @Override
                     public void run() {
                             System.out.println("Hello World!");
                     }
                   };
                   thread.start();
          }
}

Above is created by using Thread object.we can class start() method just between the  "};" by "}.start();" .

Using Runnable object

class ThreadUsingAnonymous
{
          public static void main(String[] args)
          {
                   Thread thread = new Thread(new Runnable() {
                    @Override
                   public void run() {
                             System.out.println("Hello World!");
                     }
                   });
                   thread.start();
          }
}

Above is created by using Thread object.we can class start() method just between the  ");" by ").start();" .

Output