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

Info icon This 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
Image of page 1

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

View Full Document Right 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
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.
Image of page 3

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

View Full Document Right 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
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.
Image of page 5

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

View Full Document Right 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
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.
Image of page 7

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

View Full Document Right 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.
Image of page 8
COMP201 Topic 13 / Slide 9 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!
Image of page 9

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern