Surface View 1 Multitasking and Multithreading Multitasking Multithreading

  • Slides: 14
Download presentation
Surface. View 1

Surface. View 1

Multitasking and Multithreading § Multitasking: – – § Multithreading: – – – 2 refers

Multitasking and Multithreading § Multitasking: – – § Multithreading: – – – 2 refers to a computer's ability to perform multiple jobs concurrently more than one program are running concurrently, e. g. , UNIX 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

Concurrency vs. Parallelism §CPU 3 §CPU 1 §CPU 2

Concurrency vs. Parallelism §CPU 3 §CPU 1 §CPU 2

Threads and Processes §CPU §main §run §Process 1 §Process 2 §Process §GC 4 3

Threads and Processes §CPU §main §run §Process 1 §Process 2 §Process §GC 4 3 §Process 4

Creating Threads § § 5 There are two ways to create our own Thread

Creating Threads § § 5 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

Extending Thread public class Thread. Example extends Thread { public void run () {

Extending Thread public class Thread. Example extends Thread { public void run () { for (int i = 1; i <= 100; i++) { System. out. println(“---”); } } } 6

Thread Methods § void start() – – § void run() – § The new

Thread Methods § void start() – – § void run() – § The new thread begins its life inside this method void stop() (deprecated) – 7 Creates a new thread and makes it runnable This method can be called only once The thread is being terminated

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

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) – 8 The thread sleeps for m milliseconds, plus n nanoseconds

Implementing Runnable public class Runnable. Example implements Runnable { public void run () {

Implementing Runnable public class Runnable. Example implements Runnable { public void run () { for (int i = 1; i <= 100; i++) { System. out. println (“***”); } } } 9

A Runnable Object § § § 10 When running the Runnable object, a Thread

A Runnable Object § § § 10 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

Thread State Diagram §Alive §new §New Thread(); Thread §Running §while (…) { … }

Thread State Diagram §Alive §new §New Thread(); Thread §Running §while (…) { … } §Runnable §thread. start(); §Blocked §Dead §run() Thread method returns §Object. wait() §Thread. sleep() §blocking 11 Fall 2014 CS 7020: Game Design and Development IO call §waiting on a monitor

Surface. View § A subclass of View – – – § 12 Any thread

Surface. View § A subclass of View – – – § 12 Any thread can draw Surface. Holder can manipulate a Surface. View. Surface. Holder. Callback contains methods when the Surface. View is created, changed or destroyed. Usually, an app’s user interface must be performed in the main GUI thread of execution.

Surface. Holder § Methods – – – 13 lock. Canvas: obtain the canvas for

Surface. Holder § Methods – – – 13 lock. Canvas: obtain the canvas for drawing on the Surface. View synchronized block: only one thread at a time can draw to a Surface. View unlock. Canvas. And. Post: post the change and release the canvas

Display Dialog § § 14 A dialog must be displayed from the main GUI

Display Dialog § § 14 A dialog must be displayed from the main GUI thread Call activity method run. On. Ui. Thread