In java, multithreading is a powerful feature that allows us to create and manage multiple threads of execution within a single program. Thread in Java are instances of the Thread Class or Objects that implement the Runnable Interface. Here are some commonly used methods and techniques for working with threads in Java.

Creating Thread.

In java there are two mechanisms we can use to create Java Threads.

1. Extending the Thread class

One approach is extending the Thread Class and overriding its run() method. Then you can instantiate and start the thread. However, remember that java supports Only Single Inheritance, In other words a Sub class can only extend One super class.

class MyThread extends Thread {
    public void run() {
        // Thread logic here
    }
}

public class MainThread {
    public static void main(String[] args) {
        MyThread thread = new MyThread();
        thread.start();
    }
}

2. Implementing the Runnable interface.

Another way is implementing Runnable interface and passing and instance of your class to the Thread Object

class MyThread implements Runnable {
    public void run() {
        // Thread logic here
    }
}

public class MainThread {
    public static void main(String[] args) {
        Thread thread = new Thread(new MyThread());;
        thread.start();
    }
}

Starting Threads And Stopping Threads.

  • start() method is used to begin the execution of a thread.
  • run() method contains the code that will be executed by the thread.
  • stop() method can be used to stop the thread. But not recommended to use this method because this can lead to unpredictable behaviour.
  • interrupt() method allows us to interrupt the execution of a Thread. When you call this method on a thread object, it sets the threads interrupted status to thread. This status is checked by the thread periodically, and it can be used to gracefully stop or interrupt the thread's execution.

Thread Status.

  • New : When a thread is created but not yet started.
  • Runnable : When the thread is actively executing or ready to run.
  • Blocked : When the thread is waiting for monitor lock.
  • Waiting : When the thread is in a waiting state, Typically due to wait() or join() method calls.
  • Timed_waiting : When the thread is waiting for a specified amount of time.
  • Terminated : When the thread has completed its execution.

Thread Priority.

setPriority(int priority) Sets the priority of a thread(1 to 10), 5 is default.

Thread Synchronization.

  • synchronized keyword is used to create synchronized blocks or methods to ensure exclusive access to critical sections of code.
  • wait() thread to wait until another thread notifies it.
  • notify() Wakes up one waiting thread.
  • notifyAll() Wakes up all waiting threads.

Thread Joining.

  • join() Waits for the thread to die.
  • join(long milliseconds) Waits for the thread to die or until a specified timeout.

Constructors of Thread Class.

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