Loading presentation...

Present Remotely

Send the link below via email or IM


Present to your audience

Start remote presentation

  • Invited audience members will follow you as you navigate and present
  • People invited to a presentation do not need a Prezi account
  • This link expires 10 minutes after you close the presentation
  • A maximum of 30 users can follow your presentation
  • Learn more about this feature in our knowledge base article

Do you really want to delete this prezi?

Neither you, nor the coeditors you shared it with will be able to recover it again.


Mobile Games Development (IMAT2608/3608) - Lecture 3

In this lecture, we discuss the use of threads in Android for the development of games and the game loop.

Jethro Shell

on 13 October 2014

Comments (0)

Please log in to add your comment.

Report abuse

Transcript of Mobile Games Development (IMAT2608/3608) - Lecture 3

Lecture 3 - Threads
Why Do We Need Threads?
Threads Using AsyncTask
Threads Using Handler
Thread Class
Each application has at least one thread running when it is started (main thread).
Two ways to execute a new thread:
Subclass of Thread overriding run()
Construct a new Thread and pass Runnable
start() method is called to execute the new Thread.
Simple programs execute line by line.
Gaming requires more than one thing to occur, or to be updated, at one time.
Line by line, waiting for each instruction to complete.
Ideal for simple tasks.

Real Racing 3
Thread can be used to provide this functionality.
Why do we need threads?
What is a thread?
Different approaches to using threads:
Thread Class
Examples of threads.

Mobile Games Development (IMAT2608/3608) - Lecture 3
Dr Jethro Shell

AsyncTask is part of the android.os package.
It is designed to be a helper class around
AsyncTasks should be used for short operations, just a few seconds.
For longer operations, it is recommended to use other API's such as handler or thread directly.
An asynchronous task runs on a background thread and is published on the UI thread.
This makes it thread safe.
It has four steps:
onPreExecute - invoked on the UI thread before the task is executed.
doInBckground- invoked on the background thread immediately after onPreExecute() finishes executing.
onProgressUpdate- This method is used to display any form of progress in the user interface while the background computation is still executing.
onPostExecute - invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter.
What Is a Thread?
Two concurrent programs are known as processes.
Separate tasks performed by a single program are threads.
Threads can be referred to as lightweight processes.
Threads do not completely separate areas of memory, they share code, data areas and context.
They work on time-slicing principles.
Thread 1
Thread 2
For data intensive, long running operations improvements can be made by splitting into smaller operations.
For example, decoding multiple image files to be displayed as thumbnails on separate threads.
We can use
to achieve this (optimisation).
Android UI works on a single thread.
As a result nontrivial applications need to be multithreaded.
E.G. UI responds to input despite updating from the internet, not hanging.
UI can not be ordered by long running processes.
The task is implemented simply using:
Example of a Async subclass.
Params: sent to task upon execution
Progress: units published during background
Result: units used on completion of computation
The Handler class can be used to register to a thread and provides a channel to send data to this thread.
It registers to the thread it was created in.
If created in onCreate() of your activity, the Handler object can be used to post data to the main thread.
Handler class can be an instance of the Message or the Runnable class.
Handler Class
Create a Handler object in your UI thread.
Spawn off worker threads to perform any required expensive operations.
Post results from a worker thread back to the UI thread's handler.
Update the views on the UI thread as needed.
Frontrunner Posts
Research Frontrunner in Sensor Networks
, PI: Jethro Shell (jethros@dmu.ac.uk), 6 months

Research Frontrunner – Traffic Condition Forecasting
, PI: Ben Passow (benpassow@dmu.ac.uk), 9 months

Research Frontrunner - Elastic Traffic Model
, PI: Ben Passow (benpassow@dmu.ac.uk), 9 months
Student Reps
Second year student reps for Computer Games Programming.
Two candidates needed.
More than two, election will be held.
1 min "Who am I" speech in lecture.
Reps are the key link between tutors and students.
Course reps represent student views at student staff consultative committee and programme enhancement boards.
Training given to course reps.
Well regarded, very good for C.V and employability.
Interested? - Email: jethros@dmu.ac.uk
Assignment 1
Group work: 2 -4 members of a team. Work in lab groups. Add your names to the wiki.
Constructing an "elevator pitch".
Short, concise, well rehearsed, 10 min presentation of a game concept.
Based on 2D, Shoot 'em up genre.
Inspiration from the theme: .mutation.
Submission to TurnItIn and then a presentation during
weeks 15 or 16
in the labs.
Submission date:
Friday 9th January 2015 at 12:00
Robotron 2084
Super Smash TV
Towel Fight 2
Sine Mora
Further inspiration:
Main thread is in charge of dispatching events (draw events, widgets etc).
The system does not create a separate thread for each instance of a component.
Consequently, methods that respond to system callbacks (such as onKeyDown() to report user actions or a lifecycle callback method) always run in the UI thread of the process.
If everything is happening in the UI thread, performing long operations such as network access or database queries will block the whole UI.
Even worse
, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog.
KEY POINTS: Do not block the UI thread and Do not access the Android UI toolkit from outside the UI thread.
Simple approach - construct an array.
// allocates memory for 10 integers
anArray = new int[10];

// initialize first element
anArray[0] = 100;
// initialize second element
anArray[1] = 200;
class Card
int suit, rank;

public Card () {
this.suit = 0; this.rank = 0;

public Card (int suit, int rank) {
this.suit = suit; this.rank = rank;
To create an object that represents the 3 of Clubs, we would use the new command:

Card threeOfClubs = new Card (0, 3);
class Student {
int marks;

Student[] studentArray = new Student[7];

studentArray[0] = new Student();

for ( int i=0; i<studentArray.length; i++) {
studentArray[i]=new Student();
Full transcript