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:

  1. They make sense from an organizational point of view.
  2. They are a proven way of structuring a program that works.
  3. 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.