Hi there,

I am Rohan!

""""✰I am always blown away to see thought leaders like Rohan with ambition, having a positive impact on the world!"✰ Michael Walmsley, CEO and founder of CodeAvengers.
Join Rohan Chaubey on

Multithreading In Java!

Suppose you are at your computer and you have opened Control Panel, My Computer and Mozilla Firefox simultaneously. This can be called as multi tasking. Similarly, in Java when a single program contains multiple threads to execute simultaneously is called as multi threading.

What are Threads?
Thread is a light weight process which performs a specific task.

How to create threads in programs?
This can be done using one of the following two methods :
1. Extend the thread class.
2. Implement the Runnable interface.
(Remember : We have already learned Inheritance and Interfaces)

Life Cycle Of An Thread :
During the life time of a thread, it undergoes the following states - Newborn state -> Runnable state -> Running state -> Blocked state -> Dead state.

Life Cycle Of A Thread

Extending The Thread Class :

As we have discussed, we can create threads by extending the thread class.
Steps : 1. declare the class extending the thread class. 2. Implement run() method. 3. create a thread object and call start() method to start the thread execution.

Simple Structure : 

class A extends Thread
   . . . 

class ThreadTest 

        public static void main(String args[]) 
           MyThread aThread = new MyThread();

Implementing Runnable Interface :

Steps : 1. Declare the class as implementing the Runnable interface. 2. Implement run() method. 3. Create a thread by defining an object. 4. Call the start() method to run the thread.

Simple Structure : 

Synchronized methods : enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods.

To find out more about Synchronized methods and multi threading click here!

Thank you for reading. Happy Learning.

ABOUTRohan Chaubey

Rohan Chaubey is the founder of “Be Rohanlicious” – a blog that teaches you to be smart with social media, blogging and programming.

Rohan is on a mission to help all the enthusiastic souls like you, who want to “make it to the next level” and stand out of the crowd.

His e-book “Make People Want You” is releasing soon. Allow him to inject awesomeness in your life by signing up to his list here.