Programming and Problem Solving With Java Copyright 1999, James M. Slack Threads and Animation Threads Animation.

Slides:



Advertisements
Similar presentations
Made with love, by Zachary Langley Applets The Graphics Presentation.
Advertisements

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.
Locks (Java 1.5) Only one thread can hold a lock at once –Other threads that try to acquire it block (or become suspended) until lock becomes available.
CS 11 java track: lecture 7 This week: Web tutorial:
Chapter 7 Threads  Threads & Processes  Multi Threading  Class Thread  Synchronized Methods  Wait & Notify.
Java Threads Part I. Lecture Objectives To understand the concepts of multithreading in Java To be able to develop simple multithreaded applications in.
Slides 8c-1 Programming with Shared Memory Java Threads and Synchronization Review The following notes are based upon the Java tutorial at
Multithreading Why multi-threads Defining and creating threads An example with two threads Life cycle of a thread An example with user interface Problem.
CSE S. Tanimoto Java Threads 1 Java Threads (Outline) Motivation The class Thread Example program: ThreadRace The Runnable interface Example: Clock.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
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.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Programming in Java; Instructor:Alok Mehta Threads1 Programming in Java Threads.
Slides prepared by Rose Williams, Binghamton University ICS201 Lectures 18 : Threads King Fahd University of Petroleum & Minerals College of Computer Science.
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.
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.
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.
(c) University of Washington04-1 CSC 143 Java Inheritance Example (Review)
Parallel Processing (CS526) Spring 2012(Week 8).  Thread Status.  Synchronization in Shared Memory Programming(Java threads ) ◦ Locks ◦ Barriars.
1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.
1 CSCD 330 Network Programming Lecture 13 More Client-Server Programming Sometime in 2014 Reading: References at end of Lecture.
Locks CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Object Oriented Programming Lecture 8: Introduction to laboratorial exercise – part II, Introduction to GUI frames in Netbeans, Introduction to threads.
1 Java Threads and Synchronization Review Modified from slides taken from
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
REVIEW On Friday we explored Client-Server Applications with Sockets. Servers must create a ServerSocket object on a specific Port #. They then can wait.
Multithreading. Chapter Goals To understand how multiple threads can execute in parallel To learn how to implement threads To understand race conditions.
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.
Multithreading : synchronization. Avanced Programming 2004, Based on LYS Stefanus’s slides slide 4.2 Solving the Race Condition Problem A thread must.
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.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
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.
Java Thread and Memory Model
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
SurfaceView.
Copyright by Scott GrissomCh 2 Class Definition Slide 1 Class Structure public class BankAccount{ fields constructor(s) methods } Sample Code Ticket Machine.
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Threads. Objectives You must be able to answer the following questions –What code does a thread execute? –What states can a thread be in? –How does a.
Chapter11 Concurrent. 集美大学 计算机工程学院 Java 程序设计 年 第二版 Concurrent ●Computer users take it for granted that their systems can do more than one thing.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
1 Java Programming Java Programming II Concurrent Programming: Threads ( I)
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.
Java Applets Adding Animation. Import Files You still need to include the same files: –import java.applet.*; –import java.awt.*;
Java Threads 1 1 Threading and Concurrent Programming in Java Threads and Swing D.W. Denbo.
Chapter 11: Threaded Programs Situations where the program is following multiple execution paths (how to stop one?) Thread: a line of execution Thread.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
CSCD 330 Network Programming
CSE 501N Fall ‘09 21: Introduction to Multithreading
Object-Orientated Analysis, Design and Programming
Chapter 19 Java Never Ends
Threads Chate Patanothai.
Condition Variables and Producer/Consumer
Condition Variables and Producer/Consumer
Java Threads (Outline)
Programming with Shared Memory Java Threads and Synchronization
Programming with Shared Memory Java Threads and Synchronization
Java Threads (Outline)
Computer Science 2 06A-Java Multithreading
Threads and Multithreading
CSCD 330 Network Programming
Lecture 19 Threads CSE /6/2019.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Programming and Problem Solving With Java Copyright 1999, James M. Slack Threads and Animation Threads Animation

Programming and Problem Solving With Java 2 Threads  Multitasking  Ability to run multiple programs at once  Most operating systems allow this  O/S switches between programs rapidly  O/S must protect programs from each other  Multithreading  Program can execute more than one thread of execution  New languages (like Java) allow this  Supported by O/S  Can switch threads quicker than programs -- no protection  Example: Java’s garbage collection runs as a separate thread in all Java programs

Programming and Problem Solving With Java 3 Threads: How to Use  Supported by the Thread class  In java.lang, so don’t need to import it  Two ways to use the Thread class  Write a subclass that extends Thread (but doesn’t work if you want to subclass something else, too)  Implement the Runnable interface (more complicated, but always works)

Programming and Problem Solving With Java 4 Threads: How to Use  Methods in Thread class

Programming and Problem Solving With Java 5 Threads: How to Use  Thread-related methods in Object class

Programming and Problem Solving With Java 6 Threads: How to Use  Example program -- 2 threads of one class // Demonstration of threads (both threads from the same class) class ThreadExample extends Thread { // Constructor public ThreadExample(String name) { this.name = name; } // run: Display count from 0 to 9 public void run() { for (int i = 0; i < 10; i++) { System.out.println(name + " " + i); } // Instance variable String name; } public class DemoThreads { public static void main(String args[]) { // Define two threads ThreadExample firstThread = new ThreadExample("First"); ThreadExample secondThread = new ThreadExample("Second"); // Start both threads firstThread.start(); secondThread.start(); } First 0 Second 0 First 1 Second 1 First 2 Second 2 First 3 Second 3 Second 4 Second 5 Second 6 Second 7 Second 8 First 4 Second 9 First 5 First 6 First 7 First 8 First 9

Programming and Problem Solving With Java 7 Threads: How to Use  Example program -- threads from different classes // Demonstration of threads (from different classes). class ThreadExample extends Thread { // Constructor public ThreadExample(String name) { this.name = name; } // run: Display count from 0 to 9 public void run() { for (int i = 0; i < 10; i++) { System.out.println(name + " " + i); } // Instance variable String name; }

Programming and Problem Solving With Java 8 Threads: How to Use class ThreadExample2 extends Thread { // Constructor public ThreadExample2(String name) { this.name = name; } // run: Display count from 100 to 0 by -10 public void run() { for (int i = 100; i >= 0; i = i - 10) { System.out.println(name + " " + i); } // Instance variable String name; } public class DemoThreads2 { public static void main(String args[]) { // Define two threads ThreadExample firstThread = new ThreadExample("First"); ThreadExample2 secondThread = new ThreadExample2("Second"); // Start both threads firstThread.start(); secondThread.start(); } First 0 Second 100 Second 90 Second 80 Second 70 Second 60 First 1 First 2 First 3 First 4 Second 50 Second 40 Second 30 Second 20 First 5 First 6 First 7 First 8 Second 10 Second 0 First 9

Programming and Problem Solving With Java 9 Threads: How to Use  Threads may use completely different objects  Then no problem -- threads can run independently  Threads may share objects  Then need to be careful they don’t “walk all over each other”  Example: Banking (need a couple of classes...) class BankAccounts { // Constructor: Initialize the bank account balances public BankAccounts(int numAccounts) { account = new double[numAccounts]; for (int i = 0; i < numAccounts; i++) { account[i] = 0; } // deposit: Add the given amount to the given accountNumber public void deposit(int accountNumber, double amount) { double balance = getBalance(accountNumber); balance = balance + amount; setBalance(accountNumber, balance); }

Programming and Problem Solving With Java 10 Threads: How to Use // withdraw: Subtract the given amount from the given // accountNumber public void withdraw(int accountNumber, double amount) { double balance = getBalance(accountNumber); balance = balance - amount; setBalance(accountNumber, balance); } // getBalance: Return the balance for the given accountNumber public double getBalance(int accountNumber) { return account[accountNumber]; } // setBalance: Change the balance of the given account number // to the given amount public void setBalance(int accountNumber, double amount) { account[accountNumber] = amount; } // Instance variable double[] account; }

Programming and Problem Solving With Java 11 Threads: How to Use class Customer extends Thread { public Customer(int customerNumber, int accountNum, BankAccounts bank, int depositAmount) { this.customerNumber = customerNumber; this.accountNum = accountNum; this.bank = bank; this.depositAmount = depositAmount; } // Deposit the given amount for this customer, one dollar at // a time public void run() { System.out.println("Customer-" + customerNumber + " starts depositing $" + depositAmount + " into account " + accountNum); // Deposit the money for (int i = 1; i <= depositAmount; i++) { bank.deposit(accountNum, 1.00); } System.out.println("Customer-" + customerNumber + " done depositing into account " + accountNum); } // Instance variables int customerNumber; int accountNum; BankAccounts bank; int depositAmount; } Also need a Customer class (subclass of Thread) Note: Deposits $1 at a time

Programming and Problem Solving With Java 12 Threads: How to Use public class ConcurrencyProblem { public static void main(String args[]) throws java.io.IOException { // Get the number of bank accounts and initialize them int numAccounts = Keyboard.readInt("Number of accounts: "); BankAccounts bank = new BankAccounts(numAccounts); // Get the number of customers and initialize them int numCustomers = Keyboard.readInt("Number of customers: "); Customer bankCustomer[] = new Customer[numCustomers]; for (int i = 0; i < numCustomers; i++) { int accountNumber = Keyboard.readInt("Account number that Customer-" + i + " deposits into: ", 0, numAccounts); int depositAmount = Keyboard.readInt("Amount that Customer-" + i + " deposits: "); bankCustomer[i] = new Customer(i, accountNumber, bank, depositAmount); } System.out.println();

Programming and Problem Solving With Java 13 Threads: How to Use // Start all the customers (threads) for (int i = 0; i < numCustomers; i++) { bankCustomer[i].start(); } // Make sure the main thread waits until all customers // finish depositing before continuing for (int i = 0; i < numCustomers; i++) { try { bankCustomer[i].join(); } catch (InterruptedException e) {} } // Display the bank balances System.out.println(); System.out.println("Bank Account Balances: "); for (int i = 0; i < numAccounts; i++) { System.out.println("Account: " + i + " Balance: $" + bank.getBalance(i)); }

Programming and Problem Solving With Java 14 Threads: How to Use  Sample run of the program  2 customers, each deposit $100,000 in their own accounts  (Program deposits one dollar at a time) Number of accounts: 2 Number of customers: 2 Account number that Customer-0 deposits into: 0 Amount that Customer-0 deposits: Account number that Customer-1 deposits into: 1 Amount that Customer-1 deposits: Customer-1 starts depositing $ into account 1 Customer-0 starts depositing $ into account 0 Customer-0 done depositing into account 0 Customer-1 done depositing into account 1 Bank Account Balances: Account: 0 Balance: $ Account: 1 Balance: $  No surprises so far...

Programming and Problem Solving With Java 15 Threads: How to Use  Another sample run of the program  2 customers, each deposit $100,000 into a single account Number of accounts: 1 Number of customers: 2 Account number that Customer-0 deposits into: 0 Amount that Customer-0 deposits: Account number that Customer-1 deposits into: 0 Amount that Customer-1 deposits: Customer-0 starts depositing $ into account 0 Customer-1 starts depositing $ into account 0 Customer-0 done depositing into account 0 Customer-1 done depositing into account 0 Bank Account Balances: Account: 0 Balance: $  Balance should be $200,000!  Threads walked all over each other

Programming and Problem Solving With Java 16 Threads: How to Use  What’s going on?  Expect threads to behave like real customers -- each waits for its turn  Threads don't work that way -- each tries to give the clerk a dollar and each tries to grab receipts, no matter who the receipt is for  Example, suppose balance is $ Customer-0 thread gets the balance of $ Customer-1 thread gets the balance of $ Customer-0 thread adds $1 to $100, resulting in a total of $ Customer-1 thread adds $1 to $100, resulting in a total of $ Customer-0 thread sets the balance to $ Customer-1 thread sets the balance to $101  Result should be $102, but is $101

Programming and Problem Solving With Java 17 Threads: How to Use  Solution -- use concurrency control  Allows more than one process to share resources  Most common way -- use locks  Define methods of a class synchronized  Synchronized method locks the object -- no other object can reference that object until the method is done  Bank example with synchronized methods Number of accounts: 1 Number of customers: 2 Account number that Customer-0 deposits into: 0 Amount that Customer-0 deposits: Account number that Customer-1 deposits into: 0 Amount that Customer-1 deposits: Customer-1 starts depositing $ into account 0 Customer-0 starts depositing $ into account 0 Customer-0 done depositing into account 0 Customer-1 done depositing into account 0 Bank Account Balances: Account: 0 Balance: $  Runs correctly!

Programming and Problem Solving With Java 18 Threads: How to Use  Still need to watch for deadlock  “Deadly embrace”  Two threads each have a resource the other wants  Conditions necessary for deadlock  The thread scheduler can't take the lock away from a thread  There is competition among threads for non-shareable objects  Each thread requests object locks one at a time  Can’t remove these conditions in most programs  Therefore, deadlock is a real possibility  Need to be careful when writing threaded programs

Programming and Problem Solving With Java 19 Animation  Can write programs in Java that display changing graphics  Example: Bouncing Hello Hello

Programming and Problem Solving With Java 20 Animation  Steps for Bouncing Hello program 1. Use the getSize() method to get size of frame 2. Change direction if we have reached the left or right edge of the frame 3. Change direction if we have reached the top or bottom edge of the frame 4. Change the row and column positions (for the next repaint()) 5. Use repaint() to redraw the frame 6. Use Thread.sleep() to pause execution of the program by a few milliseconds -- allows the user to see the image

Programming and Problem Solving With Java 21 Animation  Bouncing Hello Program // This program displays a frame with the word // "Hello" bouncing around. It does not use a separate thread // for the animation import java.awt.*; public class BounceHello extends Frame { // Constructor public BounceHello() { // Set the size of the frame and display it setSize(600, 200); show(); // Keep moving the "Hello" around on the screen, repainting // after each move while (true) { // Get size of frame (just in case user has changed it) Dimension frameSize = getSize();

Programming and Problem Solving With Java 22 Animation // Change horizontal direction if we hit the left // right edge of the frame if (column >= frameSize.width || column == 0) { columnStep = columnStep * -1; } // Change vertical direction if we hit the top or bottom // edge of the frame if (row >= frameSize.height || row == 0) { rowStep = rowStep * -1; } // Move the word a little column = column + columnStep; row = row + rowStep; // Redraw the frame repaint(); // Wait a little try { Thread.sleep(10); } catch (InterruptedException e) {} }

Programming and Problem Solving With Java 23 Animation // paint: Draw the word "Hello" at the current column and row public void paint(Graphics g) { g.drawString("Hello", column, row); } // Instance variables int column = 20; int row = 70; int columnStep = 1, rowStep = 1; public static void main(String args[]) { new BounceHello(); }  Program uses a single thread  Usually, good idea to put animation in its own thread  Can do other processing while animation continues

Programming and Problem Solving With Java 24 Animation  Bouncing Hello Program -- separate animation thread // This program displays a frame with the word // "Hello" bouncing around. It uses a separate thread for the // animation. The program also demonstrates how to use the // Runnable interface. import java.awt.*; class BounceHello2 extends Frame implements Runnable { // Constructor public BounceHello2() { // Set the size of the frame and display it setSize(600, 200); show(); } Note use of Runnable interface

Programming and Problem Solving With Java 25 Animation public void run() { // Keep moving the "Hello" around on the screen, repainting // after each move while (true) { // Get size of frame (just in case user has changed it) Dimension frameSize = getSize(); // Change horizontal direction if we hit the left or // right edge of the frame if (column >= frameSize.width || column == 0) { columnStep = columnStep * -1; } // Change vertical direction if we hit the top or bottom // edge of the frame if (row >= frameSize.height || row == 0) { rowStep = rowStep * -1; } // Move the word a little column = column + columnStep; row = row + rowStep;

Programming and Problem Solving With Java 26 Animation // Redraw the frame repaint(); // Wait a little try { Thread.sleep(10); } catch (InterruptedException e) {} } // paint: Draw the word "Hello" at the current column and row public void paint(Graphics g) { g.drawString("Hello", column, row); } // Instance variables int column = 20; int row = 70; int columnStep = 1, rowStep = 1; public static void main(String args[]) { Thread bounceThread = new Thread(new BounceHello2()); bounceThread.start(); }

Programming and Problem Solving With Java 27 Animation  Bouncing Hello program  This version shows how to do 2 tasks at the same time // This program displays two frames, each with // the word "Hello" bouncing around. It uses two threads, one for // each frame. import java.awt.*; public class BounceHello2 extends Frame implements Runnable { // Constructor public BounceHello2() { // Set the size of the frame and display it setSize(600, 200); show(); }

Programming and Problem Solving With Java 28 Animation public void run() { // Keep moving the "Hello" around on the screen, repainting // after each move while (true) { // Get size of frame (just in case user has changed it) Dimension frameSize = getSize(); // Change horizontal direction if we hit the left or // right edge of the frame if (column >= frameSize.width || column == 0) { columnStep = columnStep * -1; } // Change vertical direction if we hit the top or bottom // edge of the frame if (row >= frameSize.height || row == 0) { rowStep = rowStep * -1; } // Move the word a little column = column + columnStep; row = row + rowStep;

Programming and Problem Solving With Java 29 Animation // Redraw the frame repaint(); // Wait a little try { Thread.sleep(10); } catch (InterruptedException e) {} } // paint: Draw the word "Hello" at the current column and row public void paint(Graphics g) { g.drawString("Hello", column, row); } // Instance variables int column = 20; int row = 70; int columnStep = 1, rowStep = 1; public static void main(String args[]) { Thread bounceThread1 = new Thread(new BounceHello2()); Thread bounceThread2 = new Thread(new BounceHello2()); bounceThread1.start(); bounceThread2.start(); }

Programming and Problem Solving With Java 30 Animation  A little more realistic example  Two separate tasks -- bouncing Hello and a counter

Programming and Problem Solving With Java 31 Animation  BounceHelloCanvas  BounceHello converted to display on a Canvas class BounceHelloCanvas extends Canvas implements Runnable { public void run() { // Keep moving the "Hello" around on the screen, repainting // after each move while (true) { // Get size of frame (just in case user has changed it) Dimension canvasSize = getSize(); // Change horizontal direction if we hit the left or // right edge of the canvas if (column >= canvasSize.width || column == 0) { columnStep = columnStep * -1; } // Change vertical direction if we hit the top or bottom // edge of the canvas if (row >= canvasSize.height || row == 0) { rowStep = rowStep * -1; }

Programming and Problem Solving With Java 32 Animation // Move the word a little column = column + columnStep; row = row + rowStep; // Redraw the canvas repaint(); // Wait a little try { Thread.sleep(10); } catch (InterruptedException e) {} } // paint: Draw the word "Hello" at the current column and row public void paint(Graphics g) { g.drawString("Hello", column, row); } // Instance variables int column = 20; int row = 70; int columnStep = 1, rowStep = 1; }

Programming and Problem Solving With Java 33 Animation class Counter extends Canvas implements Runnable { // run: Keep incrementing the value of the counter public void run() { while (true) { counterValue++; repaint(); // Wait a little try { Thread.sleep(10); } catch (InterruptedException e) {} } // paint: Display the counter's value in the middle of the // canvas public void paint(Graphics g) { // Draw a rectangle around the outer edge Dimension canvasSize = getSize(); g.drawRect(0, 0, canvasSize.width, canvasSize.height); // Display the counter's value g.drawString("" + counterValue, canvasSize.width / 2, canvasSize.height / 2); } // Instance variables int counterValue = 0; } Counter class -- displays a counter on a Canvas

Programming and Problem Solving With Java 34 Animation public class TwoThreads extends Frame { public TwoThreads() { // Create the two component objects to put on the frame BounceHelloCanvas helloCanvas = new BounceHelloCanvas(); Counter counterCanvas = new Counter(); // Choose the layout manager and initialize it setLayout(new GridLayout(1, 2)); // Add the components to the frame add(helloCanvas); add(counterCanvas); // Set the size of the frame and display it setSize(400, 200); show(); // Create a thread for each component Thread helloThread = new Thread(helloCanvas); Thread counterThread = new Thread(counterCanvas); // Start the threads helloThread.start(); counterThread.start(); } TwoThreads class -- puts a BouncingHello and Counter on a Frame

Programming and Problem Solving With Java 35 Animation  Start everything with main() public static void main(String args[]) { new TwoThreads(); }