Home » Problem Solving » How to implement threads in Java

How to implement threads in Java

EDIT: If you liked this post or already know the two ways of implementing threads in Java – check out my follow-up Synchronization in Java

First off if you do not know what a thread is or how it differs with a process, check out my Coding Q&A and Oracle’s thread documentation. In cheap words, a thread is a line of execution in a program. The Java Virtual Machine can run multiple threads concurrently in an application.

Once you have digested the information there and are comfortable with your theoretical knowledge of threads, you can get down to business, open your favorite IDE and start to implement threads in Java with me.

Creating a thread in Java is fortunately easy. There is 2 main ways to do so:

  • - Extend the java.lang.Thread class
  • - Implement the java.lang.Runnable interface

Which one should you use? Well, extending the Runnable interface is the usual way developers do. This way, you will have to implement most of your own methods, but you can extend your own class after (Remember that in Java a class can only have 1 parent, meaning extending the Thread class, will disable your ability to extend any other classes.

So let’s run through the first way anyway and start writing some code.

public class MyThread extends Thread
{
   public void run()
   {
      System.out.println("Wow this is awesome, I'm just running my own thread. #independent ;)");
   }
}

The above example simply makes a new class that extends the Thread class and overrides the void run() method. Keep in mind that the run() method is where all the work of a thread is done.

Let’s try our second way to implement threads in Java.

public class MyRunnable implements Runnable
{
   Thread thisThread;
   public void run()
   {
      System.out.println("Amazing... I'm another thread and I can do my own thing. #YOLO");
   }
}

This second example is more widely used. In this case, note that we have the Thread class as a variable inside our thread. For your information, the Runnable interface does not do any work at all. It just serves as a guideline on implementing threads and has only one abstract method void run() that we implemented.

Furthermore, we need a way to be able to start execution of these threads in our application/process. Again, Java (being very noob-friendly in my opinon), provides an easy way for you to get down to business and start to implement threads in java. So let’s get to the code.

public static void main(String args[])
{
   MyThread firstThread = new MyThread();
   Thread secondThread = new Thread(new myRunnable());
   
   firstThread.start();
   secondThread.start();
   
   System.out.println("Wait... How many threads are running now? #Confused");
}

As you can see, in the Thread class extension case, we can construct our class directly. On the other hand on the Runnable implementation case, we have to create a thread with our Runnable as an argument.

Finally, some food for thought for you. How many threads are running after we started our 2 threads in our main() method?
Hope you enjoyed the article. Stay posted for my next update tomorrow on synchronization… And I’ll see you guys next time :)

The following two tabs change content below.
If you like one of my posts the best way to support is give it a thumbs up, comment, or share it on social media :)

Latest posts by Ajk (see all)

%d bloggers like this: