Multithreading Programming in Java

Java provide supports multithreading programming. A multithreaded program contains two parts of the program which will execute concurrently. also, Each part of program holds specific task, and it runs one after other. After each thread executions it checks concurrently. 

What is Thread?

A thread is a small part of a program which runs on specific conditions. So, Thread may access small part of memory.

Thread Priorities: –

Thread priorities is the decision which specifies the relative priority of one thread to another. As an absolute value, a priority is meaningless, because it is not sure that a higher priority thread doesn’t run any faster than lower priority thread if it is the only thread running. Instead, a thread priority is used to decide when to switch from one running thread to the next waiting thread. This is a “Context Switch”.

->A thread can preempt by a higher priority thread: Basically, as soon as higher priority thread wants to run, it   does. This is the preemptive multitasking.         

Life Cycle of Thread:-

A thread is always one of the five states in the thread. Though The life cycle of the thread is control by Java Virtual Machine(JVM). The thread states are as follows:

  1. New
  2. Runnable
  3. Running
  4. Non-Runnable (Blocked)
  5. Terminated

1)New

    A new thread is create but before invocation of start().

2)Runnable

A thread is in runnable state but after calling start ().

3)Running

 if the thread scheduler is executing a thread ,then the thread is in a runnable state.

4)Non-Runnable (Blocked)

If the thread scheduler blocked the current thread,then the blocked thread is said to be non runnable.

5)Terminated

A thread is in dead state while it is called by the run() method.

Synchronization in Multithreading Programming:

Synchronization is the capability of control the access of multiple threads to any shared program management. It is better in case, if  we want only one thread can access the memory at a time.

Why use Synchronization?

synchronization is mostly used to:-

  1.  prevent thread interference.
  2.  solve consistency problem.  

Creating A Thread: –

However, The two  ways to create a thread:

  1. Also, By extending Thread class.
  2. By implementing Runnable interface.

1)By extending Thread class:

Output: thread is running…

2)By implementing the Runnable interface:

Output: thread is running…

Thread class | Multithreading Programming

However, Thread class provide constructors and methods to perform operations on a thread. Thread class whereas can  extends Object class and implements Runnable interface.

Commonly used Constructors of Thread class:

  • Thread()
  • Thread(String name)
  • Thread(Runnable r)
  • Thread(Runnable r,String name)

Commonly used methods of Thread class:

-> public void run()->  it is used to start the action of a thread.

-> public void start(): starts the execution of the thread.

->public void sleep(long miliseconds): Causes the currently executing thread to sleep for a milliseconds

->public void join(): waits for a thread to die.

-> public void join(long Milliseconds): Also, waits for a thread to die for the specified Milliseconds.

->public int getPriority(): it returns the priority of the thread.

->public int setPriority(int priority): to change the priority of the thread.

->  public String getName(): it returns the name of the thread.

->public void setName(String name): to change the name of the thread.

->public Thread currentThread(): it returns the execution of currently running thread.

->public int getId(): returns the current id of the thread.

->public boolean isAlive(): checks if the thread is alive.

->public void suspend(): it is used to suspend the thread(deprecated).

->  public void resume(): it is used to resume the suspended thread(deprecated).

->  public void stop():  So, it makes a thread to stop.

->  public boolean isDaemon(): it tests if the thread is a daemon thread.

Conclusion :

So, The aim of multithreading is mainly to achieve the concurrent execution. Multithreading Helps a user to execute two programs simultaneously, there are pleasant features in java to achieve multithreading. Also, Even after problems caused by multithreading ,Java supports Synchronization to prevent thread interface. However, Java makes Multithreading pretty easy and it is good to go.

Leave a Comment

Your email address will not be published. Required fields are marked *