24slide_2007 - EE3206/EE5805 Java Programming &...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: EE3206/EE5805 Java Programming & Applications EE3206/EE5805 Java Programming & Applications 1 Chapter 24 Chapter 24 Multithreading Multithreading EE3206/EE5805 Java Programming & Applications EE3206/EE5805 Java Programming & Applications 2 Objectives Objectives • To understand the concept of multithreading • To understand the life cycle of a thread. • To develop task classes by implementing the Runnable interface in cases of multiple inheritance. • To create threads to run tasks using the Thread class. • To execute tasks in a thread pool. • To use synchronized methods or block to synchronize threads to avoid race conditions. • To synchronize threads using locks. • To facilitate thread communications using conditions on locks. • To use the resource ordering technique to avoid deadlock. EE3206/EE5805 Java Programming & Applications EE3206/EE5805 Java Programming & Applications 3 Threads Concept Threads Concept Multiple threads on multiple CPUs Multiple threads sharing a single CPU Thread 3 Thread 2 Thread 1 Thread 3 Thread 2 Thread 1 Thread - Single sequential flow of control within a program Why threads? •Need to handle concurrent tasks •Like OS running multiple processes at the same time •Sharing CPU time by multiple threads (managed by OS) EE3206/EE5805 Java Programming & Applications EE3206/EE5805 Java Programming & Applications 4 Thread States Thread States New Ready Thread created Finished Running start() run() Wait for target to finish join() run() returns yield(), or time out Wait for time out Wait to be notified sleep() wait() Target finished notify() or notifyAll() Time out Blocked interrupt() A thread can be in one of five states: New, Ready, Running, Blocked, or Finished. EE3206/EE5805 Java Programming & Applications EE3206/EE5805 Java Programming & Applications 5 Creating Tasks and Threads Creating Tasks and Threads // Custom task class public class TaskClass implements Runnable { ... public TaskClass(...) { ... } // Implement the run method in Runnable public void run() { // Tell system how to run custom thread ... } ... } // Client class public class Client { ... public void someMethod() { ... // Create an instance of TaskClass TaskClass task = new TaskClass(...); // Create a thread Thread thread = new Thread(task); // Start a thread thread.start(); ... } ... } java.lang.Runnable TaskClass Create a task by implementing Runnable • implement the run() method which tells what to do in this task Create a thread object for a task •start() method invoke the run() method of the given task EE3206/EE5805 Java Programming & Applications EE3206/EE5805 Java Programming & Applications 6 Example: Example: Using the Using the Runnabel Runnabel Interface to Create and Interface to Create and Launch Threads Launch Threads • Objective: Create and run three threads: – The first thread prints the letter a 100 times....
View Full Document

This note was uploaded on 01/11/2011 for the course EE 3206 taught by Professor Cwting during the Spring '07 term at City University of Hong Kong.

Page1 / 31

24slide_2007 - EE3206/EE5805 Java Programming &...

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

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