Chapter 15 Multithreading

Slides:



Advertisements
Similar presentations
1 Threads (Extra Lecture) Developing MultiThreaded Application (Part III) Overview  Waiting for Synchronized Data.  Creating Thread Groups.  Daemon.
Advertisements

Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 24 Multithreading.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 5 Multithreading and.
CS4273: Distributed System Technologies and Programming I Lecture 4: Java Threads and Multithread Programming.
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
Thread Control methods The thread class contains the methods for controlling threads Thread() create default thread Thread(target: runnable ) creates new.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L19 (Chapter 24) Multithreading.
CSE S. Tanimoto Java Threads 1 Java Threads (Outline) Motivation The class Thread Example program: ThreadRace The Runnable interface Example: Clock.
Multithreading A thread is the flow of execution, from beginning to end, of a task in a program. With Java, you can launch multiple threads from a program.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
Programming in Java; Instructor:Alok Mehta Threads1 Programming in Java Threads.
Multithreading Threads Concept
1 Threads (Part II) Threads and Concurrency (Part II) Overview  Review of Previous Lecture.  Controlling Threads and Thread State.  Example 1: Creating.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Outline §Abstract class and interface §Inner classes and initialization blocks §Equality §Exception §Threads.
Java Programming, Second Edition Chapter Seventeen Multithreading and Animation.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 19 Multithreading.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Online Appointment Book Implement a Client/Server application for an online appointment book. Client should be a Graphical User Interface application.
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Java Threads. What is a Thread? A thread can be loosely defined as a separate stream of execution that takes place simultaneously with and independently.
Threads.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
Advanced Programming 2004, based on LY Stefanus’s slides slide 8.1 Multithreading : Thread Scheduling ThreadGroup.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
In Java processes are called threads. Additional threads are associated with objects. An application is associated with an initial thread via a static.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Multithreading [Modified]
Multithreading in JAVA
Object-oriented Programming in Java. © Aptech Ltd. Introduction to Threads/Session 7 2  Introduction to Threads  Creating Threads  Thread States 
Li Tak Sing COMPS311F. Threads A thread is a single sequential flow of control within a program. Many programming languages only allow you to write programs.
© Wang Bin 2004 Java Threads. © Wang Bin 2004 In this lesson, you will learn to: u Define the concepts of threads and multithreading  Identify the functions.
Multi-Threading in Java
Multithreading. Multithreaded Programming A multithreaded program contains two or more parts that can run concurrently. Each part of such a program is.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Multithreading and Garbage Collection Session 16.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
Threads b A thread is a flow of control in a program. b The Java Virtual Machine allows an application to have multiple threads of execution running concurrently.
1 Chapter 19 Multithreading. 2 Objectives F To understand the concept of multithreading and apply it to develop animation (§19.2). F To develop thread.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
Chapter 11: Threaded Programs Situations where the program is following multiple execution paths (how to stop one?) Thread: a line of execution Thread.
Multithreading (Based on Chapter 29 (Multithreading) Liang book and Chapter 5 (Threads) Elliotte Rusty book) Dr. Tatiana Balikhina 1.
Today Threading, Cont. Multi-core processing. Java Never Ends! Winter 2016CMPE212 - Prof. McLeod1.
Today Advanced JavaFX animation and 3D demos from Oracle. Threading. Winter 2016CMPE212 - Prof. McLeod1.
Java Thread Programming
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreaded applets
Multithreading / Concurrency
Chapter 30 Multithreading and Parallel Programming
Multithreading Lec 23.
Chapter 13: Multithreading
CS244: Advanced Programming Applications
Multi Threading.
Multithreading.
PA1 Discussion.
Multithreaded Programming in Java
Chapter 19 Java Never Ends
Threads Chate Patanothai.
Multithreading.
Java Based Techhnology
Principles of Software Development
Multithreading.
Multithreaded Programming
Java Threads (Outline)
Java Threads (Outline)
Computer Science 2 06A-Java Multithreading
Multithreading in java.
Threads and Multithreading
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Chapter 15 Multithreading Threads Concept Creating Threads by Extending the Thread class Creating Threads by Implementing the Runnable Interface Controlling Threads and Thread Status Thread Groups Synchronization Creating Threads for Applets Case Studies

Threads Concept Multiple threads on multiple CPUs Multiple threads sharing a single CPU

Creating Threads by Extending the Thread class

Example 15.1 Using the Thread Class to Create and Launch Threads Objective: Create and run three threads: The first thread prints the letter a 100 times. The second thread prints the letter b 100 times. The third thread prints the integers 1 through 100.

Example 15.1 Using the Thread Class to Create and Launch Threads, cont. TestThread Run

Creating Threads by Implementing the Runnable Interface

Example 15.2 Using the Runnabel Interface to Create and Launch Threads Objective: Create and run three threads: The first thread prints the letter a 100 times. The second thread prints the letter b 100 times. The third thread prints the integers 1 through 100. TestRunnable Run

Controlling Threads and Thread States void run() Invoked by the Java runtime system to execute the thread. You must override this method and provide the code you want your thread to execute. void start() Starts the thread, which causes the run() method to be invoked. Called by the runnable object in the client class. static void sleep(long millis) throws InterruptedException Puts the runnable object to sleep for a specified time in milliseconds.

Controlling Threads and Thread States, cont. void stop() Stops the thread. (deprecated in JDK 1.2) void suspend() (deprecated in JDK 1.2) Suspends the thread. Use the resume() method to resume. void resume() (deprecated in JDK 1.2) Resumes the thread suspended with the suspend() method.

Thread Priority Each thread is assigned a default priority of Thread.NORM_PRIORITY. You can reset the priority using setPriority(int priority). Some constants for priorities include Thread.MIN_PRIORITY Thread.MAX_PRIORITY Thread.NORM_PRIORITY

Thread States

Thread Groups Construct a thread group using the ThreadGroup constructor: ThreadGroup g = new ThreadGroup("timer thread group"); Place a thread in a thread group using the Thread constructor: Thread t = new Thread(g, new ThreadClass(), "This thread");

Thread Groups, cont. To find out how many threads in a group are currently running, use the activeCount() method: System.out.println("The number of “ + “ runnable threads in the group ” + g.activeCount());

Synchronization A shared resource may be corrupted if it is accessed simultaneously by multiple threads. For example, two unsynchronized threads accessing the same bank account causes conflict.

Example 15.3 Showing Resource Conflict Objective: create and launch 100 threads, each of which adds a penny to a piggy bank. Assume that the piggy bank is initially empty.

PiggyBankWithoutSync Example 15.3, cont PiggyBankWithoutSync Run

The synchronized keyword To avoid resource conflicts, Java uses the keyword synchronized to synchronize method invocation so that only one thread can be in a method at a time. To correct the data-corruption problem in Example 15.3, you can rewrite the program as follows: PiggyBankWithSync Run

Creating Threads for Applets In Example 12.1, "Displaying a Clock," you drew a clock to show the current time in an applet. The clock does not tick after it is displayed. What can you do to let the clock display a new current time every second? The key to making the clock tick is to repaint it every second with a new current time. You can use the code given below to override the start() method in CurrentTimeApplet:

Creating Threads for Applets public void start() { while (true) stillClock.repaint(); try Thread.sleep(1000); } catch(InterruptedException ex) What is wrong in this code? As long as the while loop is running, the browser cannot serve any other event that might be occurring.

Creating a Thread to run the while loop public class MyApplet extends JApplet implements Runnable { private Thread timer = null; public void init() { timer = new Thread(this); timer.start(); } ... public void run() { ... }

Creating a Thread to run the while loop, cont. public void run() { while (true) { repaint(); try { thread.sleep(1000); waitForNotificationToResume(); } catch (InterruptedException ex) { }

Creating a Thread to run the while loop, cont. private synchronized void waitForNotificationToResume() throws InterruptedException { while (suspended) wait(); }

Creating a Thread to run the while loop, cont. public synchronized void resume() { if (suspended) suspended = false; notify(); } public synchronized void suspend() suspended = true;

Example 15.4 Displaying a Running Clock in in an Applet Objective: Simulate a running clock by using a separate thread to repaint the clock. ClockApplet Run Applet Viewer

Example 15.5 Controlling a Group of Clocks ClockGroup Clock Run