print even and odd numbers alternatively using 2 threads in java


In this article, we will learn how multi-threading works with some examples rather than going through just theory.

Prerequisites for this post.

These are the prerequisites to understand this post.

  1. What is a thread?
  2. Thread life-cycle
  3. Thread creation in Java.

If you are good with these 3 things, you can go ahead. Otherwise I recommend you to go and read about these things here.

Multi-threading

Multi threading means that you have multiple threads of execution inside the same application. A thread is like a separate CPU executing your application. Thus, a multi-threaded application is like an application that has multiple CPU's executing different parts of the code at the same time.

Multi-threading in Java is a process of executing multiple threads simultaneously making optimal use of resources available.

All the problems in the world will start to appear when multiple threads starts to read and write on a shared object at the same time. Hence, the following concepts are very important in understanding how multi-threading actually works to avoid inconsistency of data.

Concepts covered

This post covers the following concepts.

  1. Inter-thread communication.
  2. Thread synchronization.

Thread synchronization

In a multi-threaded environment, it is possible that two or more threads might be accessing a single resource at the same time which can lead to erroneous outcome.

To prevent this, we need to make sure that only one thread at a given time access the resource. This can be achieved by thread synchronization.


Inter-thread communication

How two synchronized threads should communicate with other threads is defined by Inter-thread communication.

This is done with the help of wait(), notify() and notifyAll() methods of Object class.

Wait() causes the current thread to wait indefinitely until some other thread calls notify() or notifyAll() on the same object.

More about these two topics can be found here and here.

Don’t worry, we are done with theory. We will see how these two concepts are used with multi-threading with examples.

Contents

We are going to implement the following standard problem with multi-threading which are frequently asked in interviews.

  1. Printing Even-Odd numbers using two threads.

Printing Even-Odd numbers using 2 threads

Here, we initialize two threads. One thread will print — odd numbers and the other will print — even numbers.

The catch here is when odd thread is printing, even thread has to wait and vice-versa. The two threads will access a shared resource. Each thread will call respective method for printing the numbers.

We will give odd thread a kick-start and till odd thread calls notify, even thread will be waiting for its turn.

While even thread prints, the odd thread will wait for its turn till even thread calls notify() on shared resource and the same will continue till the loop in the Task class ends.



Post a Comment