Threads – An Introduction:
What is a Thread?
Thread in java means, sequential path followed by a program in execution. All the java programs have atleast one thread (main thread).

Defining a Thread:
There are two ways of defining a thread,

  • Extending java.lang.thread
  • Implementing Runnable Interface

run():
When working with threads, it executes run method. We need to write the code that needs to be run in separate thread in run() method

Best Way for Thread:
It is always a good practice to Implement Runnable Interface. As Java does not support Multiple inheritance, If we extend thread class we cant extend any other class. So it is always better to implement Runnable.

How to Start a thread:
Even when we have code written in run() method for execution, We need Thread object to do the job for us. So in order to make thread start, We need to create thread object,

Extending thread class,
Thread thread = new Thread();
thread.start();

Implements Runnable,
Runnable runnable = new Runnable();
Thread thread = new Thread(runnable);
thread.start();

How to start multiple Threads?
We need to create multiple thread objects and use start() to start multiple threads, Here is a simple example

package Threads;

public class ThreadExample implements Runnable {

    @Override
    public void run() {
        for(int i=0; i<4; i++){
            System.out.println("Thread Running is : "+Thread.currentThread().getName());
        }
    }
    
    public static void main(String args[]){
        ThreadExample threadExample = new ThreadExample();
        Thread thread1 = new Thread(threadExample);
        Thread thread2 = new Thread(threadExample);
        Thread thread3 = new Thread(threadExample);
        thread1.setName("Thread one");
        thread2.setName("Thread two");
        thread3.setName("Thread three");
        thread1.start();
        thread2.start();
        thread3.start();
    }  
}

Output:
op
As you can see, We have started 3 threads, But the behavior is not same. Each time when you execute the code the thread output will differ.

Thread States:

  • New
  • Runnable
  • Running
  • Waiting/Sleeping/Blocked
  • Dead

New:
When thread instance has been created but start() method is not been invoked.

Runnable:
When thread is eligible to run but it is not running. When start() method is invoked thread becomes Runnable state, also when thread comes back from sleep or waiting it will be in Runnable State.

Running:
When the thread is executing, it is in Running state.

Waiting/Blocked/Sleeping:
This is the state when thread is not eligible to Run. The thread might be blocked, or waiting for some resource or developer might invoked sleep method

Dead:
Thread is said to be dead when run() method completes. Once the thread is dead, we cant start that thread again. If we try to start the dead thread using start(), it will throw an exception

Thread Priorities:
We can set priorities for the thread for execution.
         Thread thread = new Thread();
         thread.setPriority(5);

Once we set priority, at any given time thread that runs will have the highest priority than other threads.

But Still…
When two or more threads have same priority, then the behavior is not guaranteed. It depends on the scheduler to decide which thread to run.

Yield ():
Yield is a Static method.  The work of yield() is to give equals chances to other threads with same priority. That is, It will make the running thread pushed to runnable state and gives chance to another thread with same priority. But that does not mean that Same thread might be get chosen again over others. The fact is yield does not do what it claims to do.

join():
join is a non-static method. Join() allows one thread to join at the end of another thread. If Thread one cant execute until thread two is completed, Then thread one will join at the end of thread two. This causes the current thread to stop until the thread it joins completes.

By Sri

Leave a Reply

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