ch20 - Chapter Twenty: Multithreading Chapter Goals To...

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

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

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

View Full DocumentRight 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
Background 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();
Background image of page 5

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

View Full DocumentRight 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!
Background 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; }
Background image of page 7

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

View Full DocumentRight Arrow Icon
Thread Action for GreetingRunnable Print a time stamp Print the greeting Wait a second
Background 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
Background image of page 9

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

View Full DocumentRight 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
Background image of page 10
Generic run method public void run() { try { Task statements } catch (InterruptedException exception) { } Clean up, if necessary }
Background image of page 11

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

View Full DocumentRight Arrow Icon
ch20/greeting/GreetingRunnable.java 01: import java.util.Date; 02: 03: /** 04: A runnable that repeatedly prints a greeting. 05:
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 101

ch20 - Chapter Twenty: Multithreading Chapter Goals To...

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

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