CSE 341 -- S. Tanimoto Java Threads 1 Java Threads (Outline) Motivation The class Thread Example program: ThreadRace The Runnable interface Example: Clock.

Slides:



Advertisements
Similar presentations
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Advertisements

Unit 141 Threads What is a Thread? Multithreading Creating Threads – Subclassing java.lang.Thread Example 1 Creating Threads – Implementing java.lang.Runnable.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
G52CON: Concepts of Concurrency Lecture 2 Processes & Threads Chris Greenhalgh School of Computer Science
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.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Applet class The Applet class provides the essential framework that enables applets to be run by a web browser Applet do not have main method Applet depend.
Programming in Java; Instructor:Alok Mehta Threads1 Programming in Java Threads.
Threads A thread is a program unit that is executed independently of other parts of the program A thread is a program unit that is executed independently.
Java ThreadsGraphics Programming Graphics Programming: Java Threads.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Threads CS Introduction to Operating Systems.
Multithreading in Java Nelson Padua-Perez Bill Pugh Department of Computer Science University of Maryland, College Park.
Threads II. Review A thread is a single flow of control through a program Java is multithreaded—several threads may be executing “simultaneously” If you.
Applets  The Applet Class  The HTML Tag F Passing Parameters to Applets.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
Java Programming, Second Edition Chapter Seventeen Multithreading and Animation.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
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.
Today’s Agenda  Quick Review  Finish Java Threads  The CS Problem Advanced Topics in Software Engineering 1.
Concurrent Programming in Java Dr. Zoltan Papp. Motivation: event driven, responsive systems Sequential approach: while ( true ) { do event = getEventId()
1 Tutorial: CSI 3310 Dewan Tanvir Ahmed SITE, UofO.
Dr. R R DOCSIT, Dr BAMU. Basic Java : Multi Threading 2 Objectives of This Session State what is Multithreading. Describe the life cycle of Thread.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Practical OOP using Java Basis Faqueer Tanvir Ahmed, 08 Jan 2012.
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 in Java. Processes and Threads Processes –A process has a self-contained execution environment. –Has complete set of runtime resources including.
1 Web Based Programming Section 8 James King 12 August 2003.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Concurrent Programming and Threads Threads Blocking a User Interface.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
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.
Java Thread and Memory Model
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
1 Software Construction and Evolution - CSSE 375 Exception Handling – Chaining & Threading Steve Chenoweth Office: Moench Room F220 Phone: (812)
SurfaceView.
Threads Eivind J. Nordby University of Karlstad Inst. for Information Technology Dept. of Computer Science.
Multi-Threading in Java
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
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.
Threads and Multithreading. Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three general approaches:
Multithreading and Garbage Collection Session 16.
Internet Computing Module II. Threads – Multithreaded programs, thread Priorities and Thread Synchronization.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
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.
Chapter 13: Multithreading The Thread class The Thread class The Runnable Interface The Runnable Interface Thread States Thread States Thread Priority.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
Threads in Java Jaanus Pöial, PhD Tallinn, Estonia.
Multithreaded applets
Chapter 13: Multithreading
Multi Threading.
Threads Chate Patanothai.
Multithreading.
Multithreaded Programming
Java Threads (Outline)
Java Threads (Outline)
Java Thread.
Chapter 15 Multithreading
Multithreading in java.
Representation and Management of Data on the Internet
Lecture 19 Threads CSE /6/2019.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

CSE S. Tanimoto Java Threads 1 Java Threads (Outline) Motivation The class Thread Example program: ThreadRace The Runnable interface Example: Clock applet Thread state Thread priority Thread groups Application to animation

CSE S. Tanimoto Java Threads 2 Motivation Take advantage of multiprocessors Separate processing that might block on I/O or other conditions. Naturally express computations that have multiple activities going on.

CSE S. Tanimoto Java Threads 3 Forms of Parallel Processing Distributed computing: coarse-grained parallelism with relatively high communication cost (“cluster computing”) Fine-grained parallel processing: massive parallelism, single-instruction-stream multiple- data-stream processing. (e.g., for in-camera image processing). MIMD Multiprocessors with separate memories. Parallel Random Access Machines (shared memory) Multithreaded architectures (shared memory)

CSE S. Tanimoto Java Threads 4 The Class Thread Thread provides a constructor plus methods start, stop, run, interrupt, suspend, set priorities, get status, and resume threads. public Thread() -- constructor for a new thread. This or the constructor for a subclass is called by the client process to create a new thread. public void start() -- Called by client process to request that the Java runtime system run this thread instance. public void run() -- We override this method to perform the work that we want done by the thread. public void stop() -- (deprecated) Usually called by the client process to stop the thread. public void interrupt() -- Usually called by the client process to get the thread’s attention, e.g., so that it can exit its run method and thereby stop.

CSE S. Tanimoto Java Threads 5 Example: ThreadRace.java // ThreadRace.java Steve Tanimoto public class ThreadRace { public static void main( String [] args) { CountingThread aCounter = new CountingThread("a"); CountingThread bCounter = new CountingThread("b"); aCounter.start(); bCounter.start(); }

CSE S. Tanimoto Java Threads 6 Class CountingThread (for ThreadRace) public class CountingThread extends Thread { String context; public CountingThread(String context) { this.context = context; } public void run() { for (int i = 0; i < 50; i++) { System.out.print(context + i + context + " "); }

CSE S. Tanimoto Java Threads 7 Sample Output from ThreadRace C:\WINDOWS\Desktop\Threads>d:\jdk1.2.1\bin\java ThreadRace a0a b0b a1a b1b a2a b2b b3b b4b b5b b6b b7b b8b b9b b10b b11b b12b a3a b13b a4a b14b a5a b15b b16b b17b b18b b19b b20b b21b b22b b23b a6a b24b b25b a7a b26b a8a b27b a9a b28b a10a b29b a11a b30b a12a b31b a13a b32b a14a b33b a15a b34b a16a b35b a17a b36b b37b b38b b39b b40b b41b b42b b43b b44b a18a b45b a19a b46b a20a b47b a21a b48b a22a b49b a23a a24a a25a a26a a27a a28a a29a a30a a31a a32a a33a a34a a35a a36a a37a a38a a39a a40a a41a a42a a43a a44a a45a a46a a47a a48a a49a

CSE S. Tanimoto Java Threads 8 The Runnable Interface Solves the problem that we might not be able to subclass Thread because we want to subclass something else, like Applet public class myApplet extends Applet implements runnable {}

CSE S. Tanimoto Java Threads 9 Handling threads in an Applet Declare your Applet subclass to implement Runnable. public void run() {...} //Add this to do the work. What if the browser goes to another web page or quits? Provide a way for the browser to not only suspend the main thread, but also to suspend and resume any extra threads: public void start() { extraThread.resume(); } public void stop() { extraThread.timeToSleep=true; extraThread.interrupt(); } public void destroy() { extraThread.timeToQuit=true; extraThread.interrupt(); }

CSE S. Tanimoto Java Threads 10 Example: Clock Applet // Clock.java Steve Tanimoto import java.awt.*; import java.applet.*; import java.util.*; public class Clock extends Applet implements Runnable { class TimeThread extends Thread { boolean timeToQuit = false; public TimeThread(Runnable r) { super(r); } } TimeThread timeThread; public Clock() { } public void init() { timeThread = new TimeThread(this); timeThread.start(); }

CSE S. Tanimoto Java Threads 11 Clock Applet (continued) public void paint(Graphics g) { g.drawString("The time is: " + (new Date()), 100, 50); } public void run() { while (true) { try { Thread.sleep(1000); } catch (InterruptedException e) { if (timeThread.timeToQuit) return; } repaint(); } public void start() {} public void stop() {} public void destroy() { timeThread.timeToQuit = true; timeThread.interrupt(); } }

CSE S. Tanimoto Java Threads 12 Appearance of the Clock Applet The time is Fri Apr 09 01:32:00 PDT 1999 This is a test of the Clock applet.

CSE S. Tanimoto Java Threads 13 Thread State (with deprecated methods) inactive new ready running finished create start run stop suspend or sleep resume yield

CSE S. Tanimoto Java Threads 14 Thread State (with new methods) inactive new ready running finished create start run return wait, join, or sleep interrupt yield, interrupt

CSE S. Tanimoto Java Threads 15 Thread Priority 1 = MIN_PRIORITY, 10 = MAX_PRIORITY int P = myThread.getPriority(); myThread.setPriority(Thread.MAX_PRIORITY); As long as a thread of priority P is in the ready state, no thread of priority less than P will be moved to the runnable state. For threads of equal priority, CPU resources are shared, round-robin style.

CSE S. Tanimoto Java Threads 16 Thread Groups When a number of threads are to be handled in the same manner, they can be put in a thread group. ThreadGroup tg = new ThreadGroup("my sprites"); There’s a Thread constructor that takes a group as an arg. public Thread(ThreadGroup g, Runnable target, String name); There are many methods for ThreadGroup objects such as getMaxPriority, destroy, etc. ThreadGroups can contain ThreadGroups. Trees of ThreadGroups are therefore possible.

CSE S. Tanimoto Java Threads 17 Application to Animation Animation requires controlled timing. A thread that uses the sleep(int) method can handle timed updates to a display. Multiple threads can concurrent tasks, but synchronization may become an issue. Therefore a single animation thread (but separate from the main thread) often works best. The main thread handles the user interface and other activities that are logically separate from those belonging to the animation.