Java Programming, Second Edition Chapter Seventeen Multithreading and Animation.

Slides:



Advertisements
Similar presentations
Using Thread for Animation. Threads When we are doing multiple things at once we say we are multitasking Computers multitask when they run several programs.
Advertisements

Java Applets:. How Applets differ from application?: They do not use main method but init(), start() and paint() methods of the applet class They can.
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Ade Azurat, Advanced Programming 2004 (Based on LYS Stefanus’s slides) Advanced Programming 2004, Based on LYS Stefanus’s slides Slide 2.1 Multithreading.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
1 Frameworks. 2 Framework Set of cooperating classes/interfaces –Structure essential mechanisms of a problem domain –Programmer can extend framework classes,
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.
1 Threads (Part I) Introduction to Threads and Concurrency Overview  Introduction to Threads of Computation  Creating Threads in java an Example  Thread.
22-Jun-15 Threads and Multithreading. 2 Multiprocessing Modern operating systems are multiprocessing Appear to do more than one thing at a time Three.
Definitions Process – An executing program
Lecture 17: Animation Yoni Fridman 7/27/01 7/27/01.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
ITIS 1210 Introduction to Web-Based Information Systems Chapter 41 How Animation on the Web Works.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
Collage of Information Technology University of Palestine Advanced programming MultiThreading 1.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Chapter 15 Multithreading F Threads Concept  Creating Threads by Extending the Thread class  Creating Threads by Implementing the Runnable Interface.
Chapter 17: Applets, Images, and Sound. Objectives Learn about applets Write an HTML document to host an applet Use the init() method Work with JApplet.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 19 Multithreading.
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)
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Java Threads Representation and Management of Data on the Internet.
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.
1 CSC Computer Education (P) Ltd. DESIGNED BY K PRAKASH,
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.
Multithreading Chapter Introduction Consider ability of _____________ to multitask –Breathing, heartbeat, chew gum, walk … In many situations we.
Multithreading in JAVA
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
© 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
Thread basics. A computer process Every time a program is executed a process is created It is managed via a data structure that keeps all things memory.
MultiThreading Sangeetha Parthasarathy 07/10/2001.
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 & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
Multithreading. Multitasking The multitasking is the ability of single processor to perform more than one operation at the same time Once systems allowed.
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.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
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.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Threads by Dr. Amin Danial Asham. References Operating System Concepts ABRAHAM SILBERSCHATZ, PETER BAER GALVIN, and GREG GAGNE.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Programming Logic and Design Seventh Edition Chapter 12 Event-Driven GUI Programming, Multithreading, and Animation.
Java Thread Programming
Multithreading / Concurrency
Chapter 13: Multithreading
Multi Threading.
Multithreading.
Multithreaded Programming in Java
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Multithreading.
Multithreading.
Chapter 15 Multithreading
Java Programming COMP-417 Applet
Multithreading in java.
NETWORK PROGRAMMING CNET 441
APPLET PROGRAMMING.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Java Programming, Second Edition Chapter Seventeen Multithreading and Animation

In this chapter, you will: Understand multithreading Learn about a Thread’s life cycle Use the Thread class Use the sleep() method Set Thread priority

Use the Runnable interface Create an animated figure Reduce flickering Use pre-drawn animated Image objects Understand garbage collection Put animation in a Web browser page

Understanding Multithreading Thread- The flow of execution of one set of program statements Each of the programs you have written while working through this book has had a single thread Single-thread programs contain statements that execute in very rapid sequence, but only one statement executes at a time

Understanding Multithreading Multithreading- When you use a computer with multiple CPUs, the computer can execute multiple instructions simultaneously Java programming language allows you to launch, or start, multiple threads, no matter what type of processor you have

Multithreading Advantages You use multithreading to improve the performance of your programs Multithreaded programs often run faster Multithreaded programs are more user- friendly

Learning about a Thread’s Life Cycle A thread can be in one of five states during its life –New –Ready –Running –Inactive –Finished

Learning about a Thread’s Life Cycle New state- When you create a Thread The only method you can use with a new Thread is the method to start it

Learning about a Thread’s Life Cycle Ready state- When you call the Thread’s start() method A ready Thread is runnable

Learning about a Thread’s Life Cycle Running state- When a Thread begins to execute A Thread runs until it becomes inactive or finishes

Learning about a Thread’s Life Cycle Inactive state- When you call the Thread’s sleep() or suspend() method It might become inactive if it must wait for another Thread to finish and for the CPU to have available time

Learning about a Thread’s Life Cycle Finished or dead state- When a Thread completes the execution of its run() method A Thread can also enter the finished state before its run() method completes if you call the Thread’s stop() method

Using the Thread Class Every program you have created is a thread You can also create a Thread by extending the Thread class The Thread class –Defined in the java.lang package –Contains a method named run() –You override the run() method in your extended class to tell the system how to execute the Thread

Using the sleep() Method sleep() method- Use the sleep() method to pause a Thread for a specified number for milliseconds Writing sleep(500); within a Thread’s run method causes the execution to rest for 500 milliseconds When you use the sleep() method, you must catch an InterruptedException

Setting Thread Priority Every Java Thread has a priority, or rank, in terms of preferential access, to the operating system’s resources Threads with the same priority are called peers With some operating systems (like Windows), threads are timesliced; that is each peer receives a set amount of processor time during which it can execute Without timeslicing (as in the Solaris operating system), a thread runs to completion before one of its peers can execute

Setting Thread Priority Each thread object’s priority is represented by an integer in the range of 1 to 10 Default priority is 5 You can change a Thread’s priority by using the setPriority() method getPriority() lets you determine the Thread priority

Setting Thread Priority The Thread class contains three constants –MIN_PRIORITY- 1 –NORM_PRIORITY- 5 –MAX_PRIORITY- 10

Setting Thread Priority Starvation- Occurs when a Thread cannot make any progress because of the priorities of other Threads The result of starvation can be similar to creating an infinite loop The ultimate form of starvation is called deadlock

Using the Runnable Interface You can implement the Runnable interface as an alternative to inheriting from the Thread class

Creating an Animated Figure Individual drawings are shown to the audience in rapid succession to give the illusion of natural movement If you display computer images as fast as your CPU can process them, you might not be able to see anything Most computer animation employs the Thread class sleep() method to pause for short periods of time between animation cells, so the human brain has time to absorb each image’s content

Reducing Flickering When you create an applet with animation, your screen might flicker The faster your processor speed is, the less flickering you will see The trick for erasing a portion of a drawing on the screen is to use the applet’s background color to redraw the portion of the screen you want to erase To appear to move the stick figure’s arm, you draw over its old arm in the background color, and draw the new arm position in the previous drawing color

Using Pre-Drawn Animated Image Objects You can substitute a variety of more sophisticated, pre-drawn animated images to achieve the graphic effects you want within your applets Use getImage() to load animated files in your programs –Requires a URL and the name of the image file –Many animated images are available on the Web for you to use freely –Use your search engine to search for keywords such as gif files, jpeg files, and animation to find sources for shareware files

Understanding Garbage Collection Garbage collector provides for the automatic cleanup of unnecessarily reserved memory With many programming languages, when you allocate memory from the memory that is available (often known as the heap), you must purposely deallocate it or your system's performance begins to slow as less memory is available

Understanding Garbage Collection Out-of-scope objects or objects with null references are available for collection Garbage collection works by running as a very low priority thread You cannot prevent the garbage collector from running, but you request it run by using the statement System.gc();

Putting Animation in a Web Browser Page Animation makes Web pages more attractive and interesting Use HTML heading tags to display text in Web documents