π‘ A
threadis a workflow of aprocesswhere inJava,threadssharestaticandheap memorywithin theJVM.
In Java, a thread can be executed via running the main method which will invoke the main thread to run. Otherwise, to run extra threads other than the main thread, two further approaches can be introduced:
Thread ClassRunnable InterfaceThread ClassOne way is to extend a Thread Class and implement a run() method. The extending class then can be instantiated and by invoking the start() method, the designed actions can run in parallel:
Thread Class
class MyThread extends Thread {
@Override
public void run() {
System.out.println("NEW THREAD");
}
}
public class Main {
public static void main(String[] args) {
Thread thread = new MyThread();
thread.start();
}
}
Runnable InterfaceAnother way is to implement the Runnable Interface and to implement the run() method like the above approach. Executing the thread however slightly varies as it requires both Runnable class and a Thread class running the Runnable class to be instantiated:
Runnable Interface
class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("NEW THREAD");
}
}
public class Main {
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start();
}
}
π‘ A
threadcan still run with therun() methodbut the run() method will use theexisting resourceswhilestart()will assign anew resourcehence creating a newthread
It is often said that running extra threads via Runnable Interface may be more flexible and reusable followed by Java's nature that Java does not allow multiple inheritances.
The Life Cycle of Thread can be best described as the below:
β¨ New
π·ββοΈ Runnable
βοΈ Blocked
π§ββοΈ Waiting
β±οΈ Timed Waiting
ποΈ Terminated

Baeldung Available here
NewA state where a thread is just created before the start() method has been invoked.
eg
Thread thread = new Thread(runnable);
RunnableA state where a thread is is ready to run. The start() method has been invoked.
Java cannot exactly compare between a thread actually running or waiting in the scheduler's queue
eg
thread.start();
BlockedA state where a thread is waiting to acquire a monitor lock.
Blocked State implies there already is another thread running inside the synchronized block after acquiring the monitor lock.
eg
thread....
synchronized(lock) {
another thread....
}
WaitingA state where a thread indefinitely waits for other threads to complete their tasks.
Waiting can be invoked from wait(), and join() methods and can make a thread to run again by invoking notify() or notifyAll() methods.
eg
object.wait();
Timed WaitingA state that is invoked from the Thread.sleep() method. wait() and join() methods with the time parameters can also make threads fall in this state.
eg
Thread.sleep(1000);
TerminatedA state where the designed execution of a thread is complete.
Thread optimisation is an important process for improving performance in a multithreaded environment. To optimise threads, it is recommended to use a thread pool.
A thread pool pre-creates a fixed number of threads and reuses them whenever tasks becomes terminated. Hence, using a thread pool could reduce the cost or overhead associated with creating and destroying threads.
One common way to create a thread pool is via Executors as below:
eg
public class Main {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(1000);
for (int i = 0; i < 1000; i++) {
executor.submit(new MyRunnable());
}
executor.shutdown();
}
}