Threads in Java

Nilukshi Anuradha
3 min readMay 25, 2021

Threads allow concurrency (doing multiple things at the same time) within a java program.

It’s easy to understand concepts behind threads with a real-world example. Let’s think about the scenario of watching the TV while eating. That is a very simple example of performing multiple tasks at the same time. If we analyze the scenario more deeply, we can identify two separate tasks. And also they are happening with switching between those two tasks. But the time between switching can be very small. That’s why it cannot be distinguished.

Watching TV for 1, 2 seconds and concentrate on eating for 2 or 3 seconds and again start watching TV. Since we spend a small time on each task we can’t feel that switching.

We can relate this to threads in computing. Let’s think of someone implementing a java program while listening to music. There also computer performs multiple tasks.

1. Executing the java program

2. Playing the music player

There happens a swapping between two tasks, but it’s not identifiable to the human eye, because that time is too small to be identified.

Let’s try to identify a process. We can explain a process as the execution of a program. But that program can consist of several sub-tasks. Those small program units can be called threads. So one single process can have a lot of sub-tasks (Threads) running inside it.

We can implement this same thing in java, with Threads. There are two ways to create threads in java.

1. By Extending the Thread Class

· The extending class must override the run() method. Here the run() method acts as the entry point to the new thread.

· Use start() method to start and run the thread. (this method can be used to start and run the thread)

· When creating an object of the extending class (MyThread), it calls the constructor of its superclass (Thread). So the newly created object can act as an object of the Thread class.

· The main method and the run method become two threads, running on the same call stack

2. By Implementing the Runnable Interface

· Have to implement the run() method

· A Thread object should be created from the Runnable object

The main method () and run () method become two threads.

Thread Methods

Void start()

  • Creates a new thread and makes it runnable
  • This method can be called only once

· Void run()

  • The new thread begins its life inside the run method

· Void stop() (deprecated)

  • This thread is being terminated

· Void yield()

  • Temporally pauses the execution of the current thread and allows other threads to execute
  • Allow only threads of the same priority run

Each thread have a priority and represented by a number between 1 to 10

Daemon Threads: these are ‘background’ threads, provide services to other threads.

: Ex: garbage collection thread

: Daemon threads die when the JVM exits

: Thread priority is low

--

--

Nilukshi Anuradha

I’m a software engineer who love coding, analyzing, web designing and also interested in emerging technologies & new updates in the field