Skip to main content

Java Thread Class

Introduction to Java Thread Class

Java's Thread class, found in the java.lang package, is a fundamental part of the Java programming language. Threads are the smallest unit of dispatchable code; this means that threads are the smallest sequences of programmed instructions that can be managed independently by an operating system scheduler.

Multithreading, a subset of multitasking, allows a single application to have multiple threads running concurrently within a single process; these threads can perform different tasks simultaneously, improving the overall efficiency of the application.

Understanding Threads in Java

In Java, each thread is created and controlled by the Thread class. A Java application itself runs on a thread, commonly known as the main thread. When this thread completes its execution, the Java application terminates. However, you can create additional user-defined threads to perform tasks alongside the main thread.

Creating Threads

There are two ways to create a thread in Java:

  1. By extending the Thread class
  2. By implementing the Runnable interface

Extending the Thread Class

Here’s an example of how to create a thread by extending the Thread class:

class NewThread extends Thread {
public void run() {
// Code to be executed by the new thread
}
}

public class Main {
public static void main(String[] args) {
NewThread t = new NewThread();
t.start();
}
}

In the example above, we've created a new class called NewThread that extends the Thread class. We've overridden the run() method, which is where we'll include the code that we want the new thread to execute. In the main() method, we create an instance of NewThread and start it using the start() method.

Implementing the Runnable Interface

Here’s an example of how to create a thread by implementing the Runnable interface:

class NewThread implements Runnable {
public void run() {
// Code to be executed by the new thread
}
}

public class Main {
public static void main(String[] args) {
NewThread nt = new NewThread();
Thread t = new Thread(nt);
t.start();
}
}

In this example, we create a class called NewThread that implements the Runnable interface. The run() method is overridden, and we include the code to be executed by the new thread. In the main() method, we create an instance of NewThread and pass it as a parameter to the Thread constructor to create a new thread. We then start the thread using the start() method.

Thread States

A thread in Java can be in one of the following states:

  • New: A thread that has been created but not yet started.
  • Runnable: A thread that is executing in the Java virtual machine.
  • Blocked: A thread that is blocked waiting for a monitor lock.
  • Waiting: A thread that is waiting indefinitely for another thread to perform a particular action.
  • Timed Waiting: A thread that is waiting for another thread to perform an action for up to a specified waiting time.
  • Terminated: A thread that has exited.

Thread Methods

Here are some commonly used methods of the Thread class:

  • void start(): Starts the thread.
  • void run(): If the Thread class is subclassed, then this method should be overridden in the subclass. Otherwise, nothing will happen.
  • void stop(): Deprecated. Stops the thread.
  • boolean isAlive(): Tests if the thread is alive.
  • void join(): Waits for the thread to die.
  • int getPriority(): Returns the priority of the thread.
  • void setPriority(int newPriority): Changes the priority of the thread.
  • String getName(): Returns the name of the thread.
  • void setName(String name): Changes the name of the thread.

Conclusion

This was a basic introduction to the Java Thread class. Understanding threads and how to use them effectively is crucial in Java programming, especially for creating high-performing, responsive applications.

In the next step of your journey, try to create some threads yourself and play around with the methods provided by the Thread class. Happy coding!