lecture25

lecture25 - PIC 20A Lecture 25: Multithreading Java has...

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

View Full Document Right Arrow Icon
PIC 20A Lecture 25: 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
Java • has built-in multithreading or concurrency (platform dependent) • includes multithreading primitives as part of the language itself
Background image of page 2
Multithreading classes and interfaces • the most primitive classes encapsulating Java's multithreading capabilities: –Thread –ThreadGroup –ThreadLocal –ThreadDeath • interfaces –Runnable All belong to the java.lang package
Background image of page 3

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

View Full DocumentRight Arrow Icon
Thread Basics • every thread is associated with an object of class Thread • Thread implements the Runnable interface (with empty method definitions)
Background image of page 4
Runnable Interface Runnable has one method: public void run() Starting the thread object causes the object's run method to be called in that separately executing thread
Background image of page 5

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

View Full DocumentRight Arrow Icon
Creating a Thread An application that creates an instance of Thread must provide the code that will run in that thread. Ways to do this 1. Define a class that implements Runnable and pass an instance of this class into the Thread constructor class LightShow implements Runnable { public void run(){ // Thread code } } Thread lights = new Thread(new LightShow()); 2. Define a subclass of Thread and override run
Background image of page 6
What can I do with a Thread ? • start the thread: – have the Thread object call start to launch its execution and call its run method lights.start(); – after it is launched, the thread executes concurrently with the current thread Note: method start throws an IllegalThreadException if the calling Thread was already started
Background image of page 7

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

View Full DocumentRight Arrow Icon
What can I do with a Thread ? • interrupt the thread
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/12/2010 for the course PIC PIC 20A taught by Professor Keithouellette during the Spring '09 term at UCLA.

Page1 / 24

lecture25 - PIC 20A Lecture 25: Multithreading Java has...

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

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