Download presentation
Presentation is loading. Please wait.
Published byMarcus Atkinson Modified over 8 years ago
1
Programming and Problem Solving With Java Copyright 1999, James M. Slack Threads and Animation Threads Animation
2
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
3
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)
4
Programming and Problem Solving With Java 4 Threads: How to Use Methods in Thread class
5
Programming and Problem Solving With Java 5 Threads: How to Use Thread-related methods in Object class
6
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
7
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; }
8
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
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); }
10
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; }
11
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
12
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();
13
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)); }
14
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: 100000 Account number that Customer-1 deposits into: 1 Amount that Customer-1 deposits: 100000 Customer-1 starts depositing $100000 into account 1 Customer-0 starts depositing $100000 into account 0 Customer-0 done depositing into account 0 Customer-1 done depositing into account 1 Bank Account Balances: Account: 0 Balance: $100000.0 Account: 1 Balance: $100000.0 No surprises so far...
15
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: 100000 Account number that Customer-1 deposits into: 0 Amount that Customer-1 deposits: 100000 Customer-0 starts depositing $100000 into account 0 Customer-1 starts depositing $100000 into account 0 Customer-0 done depositing into account 0 Customer-1 done depositing into account 0 Bank Account Balances: Account: 0 Balance: $168556.0 Balance should be $200,000! Threads walked all over each other
16
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 $100 1. Customer-0 thread gets the balance of $100 2. Customer-1 thread gets the balance of $100 3. Customer-0 thread adds $1 to $100, resulting in a total of $101 4. Customer-1 thread adds $1 to $100, resulting in a total of $101 5. Customer-0 thread sets the balance to $101 6. Customer-1 thread sets the balance to $101 Result should be $102, but is $101
17
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: 100000 Account number that Customer-1 deposits into: 0 Amount that Customer-1 deposits: 100000 Customer-1 starts depositing $100000 into account 0 Customer-0 starts depositing $100000 into account 0 Customer-0 done depositing into account 0 Customer-1 done depositing into account 0 Bank Account Balances: Account: 0 Balance: $200000.0 Runs correctly!
18
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
19
Programming and Problem Solving With Java 19 Animation Can write programs in Java that display changing graphics Example: Bouncing Hello Hello
20
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
21
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();
22
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) {} }
23
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
24
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
25
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;
26
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(); }
27
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(); }
28
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;
29
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(); }
30
Programming and Problem Solving With Java 30 Animation A little more realistic example Two separate tasks -- bouncing Hello and a counter
31
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; }
32
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; }
33
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
34
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
35
Programming and Problem Solving With Java 35 Animation Start everything with main() public static void main(String args[]) { new TwoThreads(); }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.