In my last post we went over what a thread is and how it fits in the scope of a process. Now we are going to discuss two ways of implementing them in Java which are,
- Extending the Thread Class
- Implementing the Runnable Interface
Starting with working in the Thread class, see the code block below taken from w3schools.
public class Main extends Thread {
public void run() {
System.out.println("This code is running in a thread");
}
}
This method works by extending the Thread class and overriding its run () method. Another example is provided below for threading with classes.
public class Main extends Thread {
public static void main(String[] args) {
Main thread = new Main();
thread.start();
System.out.println("This code is outside of the thread");
}
public void run() {
System.out.println("This code is running in a thread");
}
}
Next we have an example of implementing the Runnable interface.
public class Main implements Runnable {
public void run() {
System.out.println("This code is running in a thread");
}
}
As before another more complex example using classes is provided below.
public class Main implements Runnable {
public static void main(String[] args) {
Main obj = new Main();
Thread thread = new Thread(obj);
thread.start();
System.out.println("This code is outside of the thread");
}
public void run() {
System.out.println("This code is running in a thread");
}
}
Now at this point you are probably wondering why there are two ways of implementing threading in Java. If you remember from early in your programming journey, you can only extend from one class in Java. If you extend from the Thread class that means you miss out on inheritance benefits from other classes. However, the Thread class does have some pre-built useful methods that can streamline some of your code. These are things you will need to consider when choosing how to implement threading into your project.
Runnable and Thread both use the run method. This method is where you code the functionality of the thread. When you want the thread to start executing you then call the start method for the object.
Now that we are up to speed on how to create a thread the next thing to consider is concurrency. The main benefit to threading is that they perform lightweight operations at the same time. However this can be a bad thing too. The programmer never knows the order of execution for the threads in the process. If an application was set up for multiple threads to perform actions on a variable for instance, it is impossible to know the order of operations performed on the variable. As a general rule it is best to configure threads to share as little attributes as possible. In the real world this can not always be achieved though, so there are ways of solving for concurrency and performing thread safe actions.
An example is the use of the isAlive() function in the Thread class.
public class Main extends Thread {
public static int amount = 0;
public static void main(String[] args) {
Main thread = new Main();
thread.start();
// Wait for the thread to finish
while(thread.isAlive()) {
System.out.println("Waiting...");
}
// Update amount and print its value
System.out.println("Main: " + amount);
amount++;
System.out.println("Main: " + amount);
}
public void run() {
amount++;
}
}
As you can see from this weeks post that threading in itself is not too complicated to implement but as you add threads and run into concurrency problems it can get to be a huge undertaking to keep track of. Thank you everyone for reading my post this week. Please make sure to visit the links down below for more detail and examples on how to use threading in a project. I also want to thank w3schools.com for their implementation examples that I used in the post. Please feel free to leave a like and comment if you found this helpful.
Resources: