So what is a thread? You can think of threads in Java programming in the same way you do threads in a story. In one thread of a story, we have the primary character battling the enemy on the front-line while in another thread the soldier’s family are getting by, day to day. Of course, a story doesn’t have to have just two threads. We could introduce a third thread, perhaps the story also tells of the politicians and military commanders making decisions. And these decisions then subtly, or not so subtly, affect what happens in the other threads. Threads in Java are just like this as we will discover in this quick tutorial.
[widgets_on_pages id=”udemy_advert_java_1″][widgets_on_pages id=”udemy_code_details”]
We create parts/threads in our program which control different aspects for us. We need threads to represent these different aspects because of the following reasons:
- They make sense from an organizational point of view.
- They are a proven way of structuring a program that works.
- The nature of the system we are working on forces us to use them even if we disagree with the first two points.
In Java, we use threads for all three reasons simultaneously. It makes sense, it works and we have to because of the design of the system.
In our games, we will have two threads. We will explicitly create one extra thread. I say explicitly because some of the classes we use will create their own threads and we don’t even need to know it is happening. Explicitly we will create a thread that handles the entire main game loop. That is the loop, which after we have set up our game will do just about everything. We will see exactly what this entails later in this tutorial.
This leaves the other thread which is created by default just by creating an application. This default or system thread will keep our game in touch with the operating system Android/Windows etc. This is essential because it is through this thread we will receive the player’s input; keyboard/mouse/screen presses and more. We will get a glimpse of how we handle this input when we look at interfaces in the next tutorial and we will put it into practice for real in our first game.
In gaming, think about a thread which is receiving the player’s button taps for left, right and shoot and a thread which is the alien thinking where to move next and drawing all the graphics to the screen.
In computer science, threads have a few technical challenges, very complex ones. Fortunately for us, Java provides us with a range of thread classes we can choose from to make the implementation of multiple threads really simple.
We can declare an object of the type Thread like this.
Thread gameThread;
Initialize and start it like this.
gameThread = new Thread(this); gameThread.start();
The odd looking this keyword is simply a variable which represents our application. We are passing the details of our application into a method of the Thread class which initializes our gameThread object.
We can then use the Java @override keyword to change what happens when the operating system allows our gameThread object to run its code. Here we will see the guts of just about every game we build. Within the overridden run method we call two methods that we will write in all our game projects. First is update which is where all our calculations, artificial intelligence, and collision detection will go and then draw where perhaps unsurprisingly we will draw all our graphics.
@override public void run() { // Update the game world based on // user input, physics, // collision detection and artificial intelligence update(); // Draw all the game objects in their updated locations draw(); }
When necessary we can also stop our thread like this.
gameThread.join();
Note that exactly were all these parts of threading related code will go within our code has not been explained but it is so much easier to actually show you in a real game project.
[widgets_on_pages id=”udemy_advert_java_3″][widgets_on_pages id=”udemy_code_details”]
Finally, to make this code we have just seen actually work we need to understand the topic of the final level 1 tutorial. At the same time, we will learn a bit more about that odd looking
@override keyword and where exactly the
run method seemingly magically appeared from. Let’s learn about Using Java Interfaces.
Hey, I have a recommendation for your website. I think you should add a next lesson button.
Hi John,
You are right.
At the moment, because I want the oldest tutorial shown first (easiest tutorial first) I can’t use the regular blog pagination options. When I am done with the current book, I am going to reformat everything and improve the navigation at the same time.
Thanks for your comment.
I like it but im still confused
Hi Nelson,
Feel free to ask any specific question that are causing the confusion and I will try and answer it. The best way to make things clearer is to keep reading about the topic that is challenging you as well as coding working examples. One day it will just click… and then there will be something else that is tough. Good luck and I hope you keep going.
Thank you for all the work you have put into this. Your step-by-step lessons and descriptions are just what I’ve been looking for! I hope to make this my career, but I don’t know what God will bring so I’ll have to wait and see, although I do love doing this and your lessons have been extremely helpful getting me started.
A pleasure
Hey i have no idea about programming. i would like to learn or help to build a game application… any recommendation Plz
thank you
Hi Driss,
I guess the big decision is which path (platform, programming language, game engine)you take. This is my best shot at helping people make those decisions. http://gamecodeschool.com/blog/making-games-where-do-i-start/