Java Threads & Concurrency API — Part I

What is a Thread ?

Ish Mishra
3 min readJul 12, 2021

Thread is the smallest unit of execution that can be scheduled by the OS.

What is a Task?

A task is a single unit of work performed by a Thread.

But then how do we define this work (or create a task for a thread)?

  • A common way is to use a Runnable Interface (we will implement it using Lambda expression)
Runnable runnable = () -> System.out.println("I am another Thread");
Thread anotherThread = new Thread(runnable);
anotherThread.start();
  • Or you create a Java class that implements a Runnable interface and then pass the instance of this class while creating an instance of Thread.
Thread someThread = 
new Thread(new AUserDefinedClassThatImplementsRunnable());someThread.start();
  • With a class implementing Callable Interface. Instead of overriding run() method, for this one we override call() method, which unlike run() can throw an exception and can return a generic value (a String in the example below)
public class TestCallable implements Callable<String> {    public static void main(String[] args) throws Exception {        TestCallable callable = new TestCallable();
String message = callable.call();
System.out.println(message);
}
@Override
public String call() throws Exception {
return "Task creation for thread using Callable";
}
}
  • Or by creating a class that extends Thread class. Then create an instance of this class and invoke “start()” method on it. Thread class itself implements a Runnable interface. (Avoid this way of Thread creation)
  • and also by creating Threads with the ExecutorService (discussed in detail in part 2 of this article)

What is the difference between Runnable and Callable Interface?

  • Runnable interface has an abstract “run()” method where Callable has an abstract “call()” method.
  • Runnable interface’s “run()” method has void return type whereas Callable’s abstract “call()” method can have a generic return type.
  • run() method doesn’t throw an exception while call() can throw an exception
@FunctionalInterface
public interface Callable<V> {
V call() throws Exception;
}//-------------------------------------------------------------
//-------------------------------------------------------------@FunctionalInterface
public interface Runnable {
public abstract void run();
}

What is a Process?

Now imagine a bunch of associated threads that execute in the same shared environment. That’s what a process looks like.

Types of Threads?

  • System defined threads — by the JVM and run in the background of the application (like garbage collection daemon thread)
  • User-defined threads — created by the application developer to accomplish a certain task

What is Concurrency?

The property of executing multiple threads at the same time is referred to as concurrency.
It is tricky when we say “at the same time” as in a single-core processor only one task is executing at the same time but the context switch is fast enough to give an illusion of multiple tasks being achieved at the same time. (like listening to music while preparing a Word document)

Part II in progress…

--

--