MrJazsohanisharma

MultiThreading in java

 Multithreading


Useful links:
What is multithreading:
Multithreading is a concept of running multiple threads simultaneously.

Reasons for using Multithreading in Java: Read More
  1. Parallel Programming
  2. To take full advantage of CPU power.
  3. For reducing response time
  4. To sever multiple clients at the same time.
What is Process:
A Process can have multiple threads in it. These threading can be of high priority and low priority.

What are the Threads:
Thread is a lightweight unit of a process that executes in multithreading environment.

Note: Multiprocessing and multithreading, both are used to achieve multitasking.





Why we use Thread: Read More
We use Threads to make Java applications faster by doing multiple things at the same time. 
In technical terms, Thread helps us to achieve parallelism in Java programs. Since the CPU is high-speed and it even contains multiple cores, just one Thread cannot take advantage of all the cores.

Life cycle of thread

Types of threads:
  1. User Thread/high-priority thread -> The JVM will wait for any user thread to complete its task before terminating it.
  2. Daemon Thread/low-priority thread-> daemon threads are low-priority threads whose only role is to provide services to user threads.
    • Example: main method, garbage collector
Local Definition for Daemon  and User Thread:
Daemon threads are like assistants. Non-Daemon/User threads are like front performers. Assistants help performers to complete a job. When the job is completed, no help is needed by performers to perform anymore. As no help is needed the assistants leave the place. So when the jobs of Non-Daemon threads is over, Daemon threads march away.

How to create a Thread
  1. By extending the Thread Class
  2. By implementing the Runnable interface

Signature of Thread class
public class Thread extends Object implements Runnable

Signature of Runnable interface

public interface Runnable 


When we should use  Runnable interface or Thread class?

We should use runnable interface when we are only planning to override the run() method and because this interface contains only run() method not others.

If we want to use  the multiple methods which are present in Thread calss then we should go for Thread Class.

By extending the Thread Class

class MyThread extends Thread{

  public void run()

  {

  System.out.println("concurrent thread started running..");

  }

}

classMyThreadDemo{

  public static void main(String args[])

  {

  MyThread mt = new  MyThread();

  mt.start();

  }

}

start() method: On calling start(), a new stack is provided to the thread and run() method is called to introduce the new thread into the program. Read More


run() method: 



Some Important points to Remember when we extens Thread class.

  • When we extend Thread class, we cannot override setName() and getName() functions, because they are declared final in Thread class.
  • While using sleep(), always handle the exception it throws.

static void sleep(long milliseconds) throws InterruptedException

 

Some useful public static methods are:

  1. public static native Thread currentThread();
  2. public static native void yield();
  3. public static native void sleep(long millis) throws InterruptedException
  4. public static void sleep(long millis, int nanos) throws InterruptedException
  5. public static boolean interrupted()
Some useful public non-static methods are:


What if we call Java run() method directly instead start() method? Read more
  • Each thread starts in a separate call stack.
  • Invoking the run() method from the main thread, the run() method goes onto the current call stack rather than at the beginning of a new call stack.




Thread Pool Read more

Java Thread pool represents a group of worker threads that are waiting for the job and reused many times.

In the case of a thread pool, a group of fixed-size threads is created. A thread from the thread pool is pulled out and assigned a job by the service provider. After completion of the job, the thread is contained in the thread pool again.


*

Post a Comment (0)
Previous Post Next Post