{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

ch20 - Chapter Twenty Multithreading Chapter Goals To...

Info icon This preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter Twenty: Multithreading
Image of page 1

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

View Full Document Right Arrow Icon
Chapter Goals To understand how multiple threads can execute in parallel To learn how to implement threads To understand race conditions and deadlocks To be able to avoid corruption of shared objects by using locks and conditions To be able to use threads for programming animations
Image of page 2
Threads A thread is a program unit that is executed independently of other parts of the program The Java Virtual Machine executes each thread in the program for a short amount of time This gives the impression of parallel execution
Image of page 3

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

View Full Document Right Arrow Icon
Running a Thread Implement a class that implements the Runnable interface public interface Runnable { void run(); } Place the code for your task into the run method of your class public class MyRunnable implements Runnable { public void run() { // Task statements go here . . . } } Continued
Image of page 4
Running a Thread (cont.) Create an object of your subclass Runnable r = new MyRunnable(); Construct a Thread object from the runnable object. Thread t = new Thread(r); Call the start method to start the thread. t.start();
Image of page 5

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

View Full Document Right Arrow Icon
Example A program to print a time stamp and "Hello World" once a second for ten seconds: Thu Dec 28 23:12:03 PST 2006 Hello, World! Thu Dec 28 23:12:04 PST 2006 Hello, World! Thu Dec 28 23:12:05 PST 2006 Hello, World! Thu Dec 28 23:12:06 PST 2006 Hello, World! Thu Dec 28 23:12:07 PST 2006 Hello, World! Thu Dec 28 23:12:08 PST 2006 Hello, World! Thu Dec 28 23:12:09 PST 2006 Hello, World! Thu Dec 28 23:12:10 PST 2006 Hello, World! Thu Dec 28 23:12:11 PST 2006 Hello, World! Thu Dec 28 23:12:12 PST 2006 Hello, World!
Image of page 6
GreetingRunnable Outline public class GreetingRunnable implements Runnable { public GreetingRunnable(String aGreeting) { greeting = aGreeting; } public void run() { // Task statements go here . . . } // Fields used by the task statements private String greeting; }
Image of page 7

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

View Full Document Right Arrow Icon
Thread Action for GreetingRunnable Print a time stamp Print the greeting Wait a second
Image of page 8
GreetingRunnable We can get the date and time by constructing a Date object Date now = new Date(); To wait a second, use the sleep method of the Thread class sleep(milliseconds ) A sleeping thread can generate an InterruptedException Catch the exception Terminate the thread
Image of page 9

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

View Full Document Right Arrow Icon
Running Threads sleep puts current thread to sleep for given number of milliseconds Thread.sleep(milliseconds) When a thread is interrupted, most common response is to terminate run
Image of page 10
Generic run method public void run() { try { Task statements } catch (InterruptedException exception) { } Clean up, if necessary }
Image of page 11

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

View Full Document Right Arrow Icon
ch20/greeting/GreetingRunnable.java 01: import java.util.Date; 02: 03: /** 04: A runnable that repeatedly prints a greeting. 05: */ 06: public class GreetingRunnable implements Runnable 07: { 08: /** 09: Constructs the runnable object. 10: @param aGreeting the greeting to display 11: */ 12: public GreetingRunnable(String aGreeting) 13: { 14: greeting = aGreeting; 15: } 16: 17: public void run() 18: { 19: try 20: {
Image of page 12
ch20/greeting/GreetingRunnable.java (cont.) 21: for ( int i = 1 ; i <= REPETITIONS; i++) 22: { 23: Date now = new Date(); 24: System.out.println(now + " " + greeting); 25: Thread.sleep(DELAY); 26: } 27: } 28: catch (InterruptedException exception) 29: { 30: } 31: } 32: 33: private String greeting; 34: 35: private static final int REPETITIONS = 10 ; 36: private static final int DELAY = 1000 ; 37: }
Image of page 13

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

View Full Document Right Arrow Icon
Image of page 14
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