14
SurfaceView 1

SurfaceView 1. Multitasking and Multithreading Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

Embed Size (px)

Citation preview

Page 1: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

1

SurfaceView

Page 2: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

Multitasking and Multithreading Multitasking:

– refers to a computer's ability to perform multiple jobs concurrently

– more than one program are running concurrently, e.g., UNIX

Multithreading:– A thread is a single sequence of execution within a

program– refers to multiple threads of control within a single

program– each program can run multiple threads of control

within it, e.g., Web Browser

2

Page 3: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

Concurrency vs. Parallelism

3

CPU CPU1 CPU2

Page 4: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

Threads and Processes

4

CPU

Process 1 Process 3 Process 2 Process 4

main

run

GC

Page 5: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

Creating Threads

There are two ways to create our own Thread object

1. Extend the Thread class and instantiating a new object of that class

2. Implementing the Runnable interface

In both cases the run() method should be implemented

5

Page 6: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

Extending Thread

public class ThreadExample extends Thread {

public void run () {

for (int i = 1; i <= 100; i++) {

System.out.println(“---”);

}

}

}

6

Page 7: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

Thread Methods

void start()– Creates a new thread and makes it runnable– This method can be called only once

void run()– The new thread begins its life inside this method

void stop() (deprecated)– The thread is being terminated

7

Page 8: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

Thread Methods

void yield()– Causes the currently executing thread object to

temporarily pause and allow other threads to execute

– Allow only threads of the same priority to run

void sleep(int m) or sleep(int m, int n)  – The thread sleeps for m milliseconds, plus n

nanoseconds

8

Page 9: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

Implementing Runnable

public class RunnableExample implements Runnable {

public void run () {

for (int i = 1; i <= 100; i++) {

System.out.println (“***”);

}

}

}

9

Page 10: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

A Runnable Object

When running the Runnable object, a Thread object is created from the Runnable object

The Thread object’s run() method calls the Runnable object’s run() method

Allows threads to run inside any object, regardless of inheritance

10

Page 11: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

Thread State Diagram

11

Alive

New Thread Dead Thread Running

Runnable

new Thread();

run() method returns

while (…) { … }

Blocked Object.wait() Thread.sleep() blocking IO call waiting on a monitor

thread.start();

Fall 2014 CS7020: Game Design and Development

Page 12: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

SurfaceView

A subclass of View – Any thread can draw– SurfaceHolder can manipulate a SurfaceView. – SurfaceHolder.Callback contains methods when

the SurfaceView is created, changed or destroyed.

Usually, an app’s user interface must be performed in the main GUI thread of execution.

12

Page 13: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

SurfaceHolder

Methods– lockCanvas: obtain the canvas for drawing on the SurfaceView

– synchronized block: only one thread at a time can draw to a SurfaceView

– unlockCanvasAndPost: post the change and release the canvas

13

Page 14: SurfaceView 1. Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one

Display Dialog

A dialog must be displayed from the main GUI thread

Call activity method runOnUiThread

14