# Multithreaded programming in Java

> [![Mentoring](https://cdn.mentorcruise.com/img/banner/navy-mentoring-badge.svg align="left")](https://mentorcruise.com/mentor/adriankodja/)
> 
> While doing multithreaded programming in Java, you would need to have the following concepts really understood and have an idea of what they do:
> 
> 1. What is thread synchronization?
>     
> 2. Handling interthread communication
>     
> 3. Handling thread deadlock
>     
> 4. Major thread operations
>     

There are 2 ways on how you can implement **multithreading** in Java:

* By extending the **Thread** class
    
* By implementing the **Runnable** class
    

Both have their pros and cons so lets dig into it and take a look at how to do it both ways.

## 1\. Extending the Thread class

```java
public class MultithreadingExample extends Thread{

  @Overrid
	public void run(){
	  for(int i=1; i<=5; i++){
			System.out.println(i);
			try{
				Thread.sleep(1000);
			}catch(InterruptedException e){}
		}
  }
}

//run it like this

public static void main(String[] args) {
	MultithreadingExample multiThreading = new MultithreadingExample();
	multiThreading.start();
}
```

---

## 2\. Implementing the Runnable

```java
public class MultithreadingExample implements Runnable{

  @Overrid
	public void run(){
	  for(int i=1; i<=5; i++){
			System.out.println(i);
			try{
				Thread.sleep(1000);
			}catch(InterruptedException e){}
		}
  }
}

//run it like this

public static void main(String[] args) {
	MultithreadingExample multiThreading = new MultithreadingExample();
	Thread thread = new Thread(multiThreading);
	thread.start();
}
```

---

Both examples will produce the same results. But if you extend the **Thread** class, the downside is that you will not be able to extend any other class. But if you implement the **Runnable** interface, then you could still extend any other class but implement multiple interfaces because Java allows that.

[#programming](https://www.linkedin.com/feed/hashtag/?keywords=programming&highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6888145364537495552) [#java](https://www.linkedin.com/feed/hashtag/?keywords=java&highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6888145364537495552) [#threads](https://www.linkedin.com/feed/hashtag/?keywords=threads&highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6888145364537495552) [#asynchronous](https://www.linkedin.com/feed/hashtag/?keywords=asynchronous&highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6888145364537495552) [#coding](https://www.linkedin.com/feed/hashtag/?keywords=coding&highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6888145364537495552) [#javascript](https://www.linkedin.com/feed/hashtag/?keywords=javascript&highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6888145364537495552) [#multithreading](https://www.linkedin.com/feed/hashtag/?keywords=multithreading&highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6888145364537495552) [#go](https://www.linkedin.com/feed/hashtag/?keywords=go&highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6888145364537495552)

[#softwareengineering](https://www.linkedin.com/feed/hashtag/?keywords=softwareengineering&highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6888145364537495552) [#softwarearchitecture](https://www.linkedin.com/feed/hashtag/?keywords=softwarearchitecture&highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6888145364537495552) [#designpatterns](https://www.linkedin.com/feed/hashtag/?keywords=designpatterns&highlightedUpdateUrns=urn%3Ali%3Aactivity%3A6888145364537495552)

Thanks for reading!
