Multithreaded programming is a big part of Java. It’s been a part of the Java core since the early days. It’s one of the reasons why Java is the most popular programming language. We make a second thread when we want to do some task parallelly with the main task. You always have at least one thread running in your program. The Android API uses multithreading heavily. The most common example is the OnClickListener you put on your buttons or on whatever else you want. It’s an asynchronous task that constantly checks for clicks on the selected View.
If you are a beginner to the Android API or even Java, you don’t need to worry about making new threads for your app. The Android API takes care of most of the heavy lifting. Most of the time you’ll be implementing some of the methods already provided by the Android API. The perfect example is the OnClickListener. You do not need to write your own code to do things like that. Android API provides solutions for most of the problems you will encounter. You are not a beginner in my mind when you are able to write you own threads.
Basic Java Multithreading Program
First we’ll write a basic Java command line program that has multiple threads.
In this example we’re implementing the Runnable interface. Let’s go through the code step by step.
First, we’re making a constructor for the MyThread class which implements the Runnable interface. The constructor sets the name of the thread and starts it. Next is the method run(). That is the only method that you must override in order to be able to write multithreaded programs. The same is the case when extending the Thread class. Inside the run() method, first we’re printing the name of the thread. Next we have a try and catch blocks. That is the case because we’ve called the Thread.sleep() method. That method throws a InterruptedException. By calling the sleep() method, we’re telling the thread to wait for a certain amount of time. In this case it is set to 40 milliseconds. There is no practical use of the sleep() method in this example. I’ve put there so that it would be simpler to see how the threads switch places, but then I set the for loop to go to 10 000. Not really the best move.
In the main class first we made three MyThread objects. Then we’ve set the priority of each of the threads. Normal priority that every thread has (Thread.NORM_PRIORITY) is 5. So the Thread#2 has the priority of 5. Thread#1 has priority of 1 (because Thread.MIN_PRIORITY has value of 1) and Thread#3 has value of 10 (also because Thread.MAX_VALUE has the value of 10).
The join() method joins the selected thread to the main thread. That basically means that the main thread will wait for the other thread to finish first before itself can finish.
This is a small part of the command line output:
Extending the Thread class
This is the previous code changed so that the MyThread class extends the Thread class:
Why Use Runnable Over Thread
In a lot of cases, these two are the same. The most basic reason why most of Java developers implement the Runnable interface rather than extend the Thread class is that when implementing the Runnable interface you can still extend some other class and add extra functionality to your program. Other than that it is basically the same. Most of the time you will not need to worry about whether you should extend or implement. Just implement is like most Java developers do and you’ll have no problems. At least I hope so.