Threads in Java

Asam hussain
1 min readMar 1, 2022

Thread is flow of execution. Threads allows a program to operate more efficiently by doing multiple things at the same time. Multi-threading means multiple flow of execution(multi tasking).

There are two types of multi-tasking

  1. Process based multi-tasking- our operating system can run multiple programs( different processes) at same time even if it runs at same time they are independent from each other so by killing on program(thread) there is no changes in other programs .

2. Thread based multi-tasking- .These threads are belongs same main process which runs in an operating system . So operating system thinks there is only one thread /process .

There are two ways to create thread

  1. Extend Thread class

2. implements Runnable interface

run method

if we extend Threads then we can override run method or not override run method the program will execute. but here what ever job we have to do using threads we have to write it within the run methods .if we don’t have any run method there won’t be any job .If we use runnable interface that force us to override run method otherwise it will show error and program won’t run .

start method

The start() method of thread class is used to begin the execution of thread. The start thread performs the following tasks:

  1. It starts a new thread
  2. Thread goes from new state to runnable state
  3. When the thread gets a chance to execute, its target run() method will run.

--

--