• $10 Game Dev' Courses - Use code "BREAKTHRU"

    Video courses

The Complete Java Developer Course. Learn Step by Step

The Complete Android & Java Developer Course – Build 21 Apps

Master Android N App Development With Java

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.

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.

  • Learning java by building Android games

    Learning Java by Building Android Games

    Want to learn Java for Android? Even if you are completely new to either Java, Android, or game programming but are aiming to publish Android games, then this book is for you. This book also acts as a refresher for those who already have experience in Java on other platforms or other object-oriented languages.
    • Setup your own Android game programming environment using Android Studio
    • Control logic, branch your code, and add real decision-making depth to your games
    • Design and use 2d sprite animations, smooth pixel graphics, sound FX, simple collision detection and artificial intelligence (AI)
    • Build around a dozen sample test apps and 4 complete working games!
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US only)

    Android Game Programming by Example

    Learn to build exciting Android game projects. Build a simple flappy-bird like game to a multi-environment, tough, retro platformer then an OpenGL ES 2 Asteroids clone, running at hundreds of frames per second .
    • Animate your characters with sprite sheets, add multiple scrolling parallax backgrounds, and implement genuinely tough, playable levels in your games
    • Every single line of code is printed in the book! No need to refer to the code files while trying to follow along. All the code files are also supplied separately so you can refer to them in their completed form and copy/paste them into your project if you like.
    • Implement a multitude of other game features such as pickups, firing weapons, HUD’s, generating and playing sound FX, scenery, level transition, high scores, and more
    Amazon USA
    Amazon UK
    Amazon CA
    Our Store (US Only)

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);

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.

public void run() {

	// Update the game world based on
	// user input, physics,
	// collision detection and artificial intelligence

	// Draw all the game objects in their updated locations


When necessary we can also stop our thread like this.


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.

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.