Multithreading in java

Tags: java, codenewbie, oop
Published: 2020-10-10 09:20:49

Multithreading  is the one which we often come across in java. So, What actually Multithreading is?, What it does?, why it is needed?.

 Let us consider the following code


```java
public class Multithreading{
          public static void main(String args[]){
             System.out.println("hello");
}
}
``` 
In the above code we can see Output is "hello"

So how this code is running? , Every Java programme has main Method and the main method gets executed. Here main method is a thread.

## How to create Threads in Java?

Now Let us make multiple threads in java
```java
class A extends Thread{
    public void run(){
        for( int i=0;i<5;i++){
        System.out.println("hi");
    }
    }
}
class B extends Thread {
    public void run(){
        for(int i=0;i<5;i++){
        System.out.println("hello");
        }
    }
}
public class multithreading{
    public static void main(String args[]){
            A obj=new A();
            B obj1=new B();
            obj.start();
            obj1.start();
    }
}

```java
In the above code ,We have two classes :
- Class A
- Class B

In order to provide a single thread for each class. We need to extend the Thread class and the code which thread should work upon will be written in the run() method and how do we call this run method? .We create a Object for each class and we call start() method. start()method is located in the Thread class. This start() method starts the execution of the thread. 


The output is different in different cases as we can't print different outputs at the same time .One is choosen between them based on some criteria and priorities.

```java 
//output1
hi
hello
hi
hello
hi
hello
hi
hello
hi
hello

//output2
hi
hello
hello
hi
hello
hi
hi
hello
hi
hello

``` 

Now we have 3 Threads

- class A thread
- class B thread
- main() thread

we should not override the start() method in Thread class by defining it  in class A and B. This calls the start() method in the child class rather than parent class and which no longer acts like a thread.

## What's the problem here?
 
So we have a problem here :(. What if I want to extend both Thread and other class. Let's say I want other  class C  which extends class A and Thread. 

```java
class C extends A,Thread{
           //code
}
``` 
Wait!. This is multiple Inheritence. Unfortunately we don't have multiple Inheritence in Java. Then how to overcome this?.

Multiple Inheritence can be overcome by Interfaces. We do have a interface called Runnable Interface in Which a class can implement Runnable Interface.


```java
class C extends A implements Runnable{
      //code
}
``` 
We are not getting into the Runnable Interface. We will discuss it in the next Article in depth. 


## Applications of Multithreading

There are lots of applications of Multithreading. Let's say you are playing game like PUBG. You can move and your friend can move and the sound effects when someone shoot these actions are performed simultaneously by different threads.

## Summary

- Threads are like workers who are need to construct a building
- Multiple Threads can work at a same time.
- Class should extends the Thread class and should have a run() method defined inorder to work as a seperate Thread.
- Create a object(obj) for the class. Call the run() method by obj.start()


Thanks for the read!. If something feels wrong, please let me know.

Happy coding :)