lec13 - COMP201 Java Programming Part III: Advanced...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP201 Java Programming Part III: Advanced Features Topic 13: Multithreading Volume II,Chapter 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 13 / Slide 2 Outline Introduction: Why and what Basics: creating and running threads Issues Thread states Thread scheduling Synchronization Suspending and stopping threads Uses Animation Threads and Swing
Background image of page 2
COMP201 Topic 13 / Slide 3 Introduction class Bounce extends JFrame { // “Start” button to start ball bouncing // “Colse” button to dispose window } class Ball { public void bounce () // bounce ball 1000 times { draw(); for ( int i = 1; i <= 1000 ; i++) { move(); try { Thread.sleep(5); } catch (InterruptedException e) {} }} } //Bounce.java Cannot start another ball before the current one finishes and cannot dispose window until all balls stop.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 13 / Slide 4 Introduction The program has a single thread of control . Actions are executed one by one. Cannot execute next action before current action finishes Implications in general: Cannot do anything else while waiting data from the net. Cannot stop downloading even though you know, after seeing part of the download, that you don’t want the download any more Solution: Multithreading
Background image of page 4
COMP201 Topic 13 / Slide 5 Introduction A multithreaded program has multiple threads of control (appears to do multiple tasks at the same time). Programs that can run more than one thread at once are said to be multithreaded. OS runs one thread a short period of time, then switches to another, and so on. To user, threads appear to run simultaneously. An illusion.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 13 / Slide 6 Introduction In our example, we need more than one thread: Main thread that listens for button clicks One thread to bounce each ball // BounceThread.java
Background image of page 6
COMP201 Topic 13 / Slide 7 Creating and Running Threads Technically, a thread is an object of a class that is a subclass of java.lang.Thread Basic methods of Thread Thread(): constructs a thread void run(): a method that you override to specify what you want the thread to do void start(): starts the thread (does setup and calls run ) static void sleep( long millis): static method. Thread.sleep(5) puts the running thread to sleep for 5 milliseconds.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 13 / Slide 8 Creating and Running Threads In our example, we want a separate thread to bounce each ball. So we Make class Ball a subclass of Thread class Ball extends Thread {..} Override the run method to bounce the ball (replaces the old bounce method). public void run() { draw(); for ( int i = 1; i <= 1000; i++) { move(); try { Thread.sleep (5); } catch(InterruptedException e) {} }} Note: sleep is a static method that puts the currently running thread to sleep. It throws InterruptedException when interrupted.
Background image of page 8
Creating and Running Threads Create a new thread: Ball b = new Ball (ballPanel); Run the new thread: b.start(); Note: calling run() directly does not start a new thread! BounceThread.java
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 47

lec13 - COMP201 Java Programming Part III: Advanced...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online