Multi threading in java

Multithreading in Java is a process of executing multiple threads simultaneously.

A thread is a lightweight sub-process, the smallest unit of processing. Multiprocessing and multithreading, both are used to achieve multitasking.

However, we use multithreading than multiprocessing because threads use a shared memory area. They don’t allocate separate memory area so saves memory, and context-switching between the threads takes less time than process.

Multitasking

Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU. Multitasking can be achieved in two ways:

  • Process-based Multitasking (Multiprocessing)
  • Thread-based Multitasking (Multithreading)
1) Process-based Multitasking (Multiprocessing)
  • Each process has an address in memory. In other words, each process allocates a separate memory area.
  • A process is heavyweight.
  • Cost of communication between the process is high.
  • Switching from one process to another requires some time for saving and loading registers, memory maps, updating lists, etc.
2) Thread-based Multitasking (Multithreading)
  • Threads share the same address space.
  • A thread is lightweight.
  • Cost of communication between the thread is low.

What is Thread Priority?

When we create a thread, we can assign its priority. We can set different priorities to different Threads but it doesn’t guarantee that a higher priority thread will execute first than a lower priority thread. The thread scheduler is the part of Operating System implementation and when a Thread is started, its execution is controlled by Thread Scheduler and JVM doesn’t have any control over its execution.

Thread Life Cycle in Java

We can create a thread in java and start it but how the thread states change from Runnable to Running to Blocked depends on the OS implementation of thread scheduler and java doesn’t have full control on that.

New

When we create a new Thread object using new operator, thread state is New Thread. At this point, thread is not alive and it’s a state internal to Java programming.

Runnable

When we call start() function on Thread object, it’s state is changed to Runnable. The control is given to Thread scheduler to finish it’s execution. Whether to run this thread instantly or keep it in runnable thread pool before running, depends on the OS implementation of thread scheduler.

Running

When thread is executing, it’s state is changed to Running. Thread scheduler picks one of the thread from the runnable thread pool and change it’s state to Running. Then CPU starts executing this thread. A thread can change state to Runnable, Dead or Blocked from running state depends on time slicing, thread completion of run() method or waiting for some resources.

Blocked/Waiting

A thread can be waiting for other thread to finish using thread join or it can be waiting for some resources to available. For example producer consumer problem or waiter notifier implementation or IO resources, then it’s state is changed to Waiting. Once the thread wait state is over, it’s state is changed to Runnable and it’s moved back to runnable thread pool.

Dead

Once the thread finished executing, it’s state is changed to Dead and it’s considered to be not alive. Above are the different states of thread. It’s good to know them and how thread changes it’s state. That’s all for thread life cycle in java.

Java thread methods

S.N.Modifier and TypeMethodDescription
1)voidstart()It is used to start the execution of the thread.
2)voidrun()It is used to do an action for a thread.
3)static voidsleep()It sleeps a thread for the specified amount of time.
4)static ThreadcurrentThread()It returns a reference to the currently executing thread object.
5)voidjoin()It waits for a thread to die.
6)intgetPriority()It returns the priority of the thread.
7)voidsetPriority()It changes the priority of the thread.
8)StringgetName()It returns the name of the thread.
9)voidsetName()It changes the name of the thread.
10)longgetId()It returns the id of the thread.
11)booleanisAlive()It tests if the thread is alive.
12)static voidyield()It causes the currently executing thread object to pause and allow other threads to execute temporarily.
13)voidsuspend()It is used to suspend the thread.
14)voidresume()It is used to resume the suspended thread.
15)voidstop()It is used to stop the thread.
16)voiddestroy()It is used to destroy the thread group and all of its subgroups.

References

https://www.javatpoint.com/multithreading-in-java

https://www.digitalocean.com/community/tutorials/multithreading-in-java

Leave a comment

Design a site like this with WordPress.com
Get started