Sitemap

Threads

3 min readSep 9, 2022

Multi-tasking in Threads

There are two types of multi-tasking in Threads

1. Process-based - In this type of multitasking, it allows two or more processes to run concurrently.

2. Thread-based - In this type of multitasking, it allows two or more threads can be run concurrently.

Creating Threads

There are two ways to create a thread.

  1. extend thread class

public class Main extends Thread {
public void run () {
System.out.println(“From Extended Thread class”);
}
}

2. implement runnable interface

public class Main implements Runnable {
public void run() {
System.out.println(“From Extended Thread class”);
}
}

When we extend Thread class, we can’t extend any other class due to any reason. When we implement Runnable, we can save a space for our class to extend any other class in future or now.

Each of our threads creates a unique object and is associated with it when we extend the Thread class. The same object is shared among numerous threads when Runnable is implemented.

Thread Priority

Get Supuni Gallege’s stories in your inbox

Join Medium for free to get updates from this writer.

Every thread is given a priority. Thread priority is not index it is a value.so thread priority starts with 1 and ends with 10. highest priority is 10 lowest priority is 1.The thread scheduler typically schedules the threads based on their priority.

However, it cannot be relied upon because the JVM specification determines the scheduling option. Be aware that not only Java programmers use JVM.
can directly assign a thread’s priority in a Java program.

when it comes to thread execution, a thread with a high priority will take precedence over threads with a lower priority. Two threads may, however, share the same priority under other circumstances. All of the processing, in order to look after the threads, is done by the Java thread scheduler.

Life cycle of a Thread

A Thread can have 6 states.It always exists in any one of them

  1. New-A thread is in the new state when it hasn’t run yet. This is the state a new thread begins in when it’s created. A thread hasn’t run yet when it’s in this state; it hasn’t started executing its code.
  2. Runnable-This when thread is either running or ready for execution.When using multithreading, a software program gives each unique thread a specific amount of time to run. Each thread runs for a certain amount of time before willingly handing control of the CPU to another thread. The next thread then finishes running its allotted time. All threads that are ready to run and are currently waiting in a queue are in the runnable state. When such an event occurs, threads are stored in a queue; they become stored in the runnable state when they’re activated.
  3. Blocked /Waiting-When a thread is temporarily inactive, then it’s either in Blocked state or Waiting state
  4. Timed Waiting-When a thread calls a method with a time-out parameter, it is in a timed waiting state. The timeout is not over or a notification is not received before a thread exits this state. A thread is put into a timed waiting state, for instance, when it calls sleep or a conditional wait.
  5. Terminated-When the thread has completed its execution it comes to terminated state.It simly means that it is no longer active in the system. In other words, the thread is dead

Java Thread join

Until the given thread is dead, the current thread’s execution can be paused using the Java Thread join method. There are three join functions that are overloaded.

1.public final void join()

2.void join(long millis

3.public final synchronized void join(long millis, int nanos)