Download presentation
Presentation is loading. Please wait.
Published byCecilia Wilkinson Modified over 8 years ago
1
CS 100Lecture 171 Announcements P4 due Thursday FINAL: Tuesday, August 10th, 8AM -- Currently scheduled for Olin 155
2
CS 100Lecture 172 Today’s Topics Control programs and Synchronization 2D arrays in Java Examples w/2D arrays in Java
3
CS 100Lecture 173 Sequential vs. Concurrent Programs we’ve done so far are sequential: one statement executes after another In concurrent programs, more than one thing is happening at the same time A thread is a sequential flow of execution through a program that occurs at the same time another sequential flow of execution is running the same program Not necessarily the same statements at the same time, though
4
CS 100Lecture 174 Java example of Threads class Soda extends Thread { private String name; Soda(String str) { name = str; } public void run() { for(int count = 0; count < 100; count++) System.out.println(name); }
5
CS 100Lecture 175 Public class Simultaneous { public static void main(String[] args) { Soda one = new Soda(“Coke”); Soda two = new Soda(“Pepsi); Soda three = new Soda(“Diet Coke”); one.start(); two.start(); three.start(); }
6
CS 100Lecture 176 Comments on previous program Where does the method start come from? –A thread begins executing when its start method is invoked –start creates a separate thread and calls run –Note that run was overridden in the Soda subclass What does the output look like?
7
CS 100Lecture 177 Output Pepsi Coke Diet Coke Pepsi Coke Pepsi Diet Coke...
8
CS 100Lecture 178 Any problems with threads? What if multiple threads are sharing data in a program? Canonical example: ATM machines You withdraw $$ at the exact same time co- owner of the account withdraws $$... Two threads operating on the same data... Need a way to guarantee that threads do not access shared data at the same time
9
CS 100Lecture 179 Synchronization synchronize is yet another method modifier If applied to the definition of a method, only one thread can be executing that method at a time Note: we can’t guarantee what order the threads will have access, just that once a thread is executing a method, no other thread can start that method
10
CS 100Lecture 1710 class Savings_Account { private int account; private int balance; public Savings_Account(int id, int init) { account = id; balance = init; } public synchronized boolean withdraw(int amount) { boolean result = false; if (amount <= balance) { balance -= amount; result = true; } else System.out.println(“no money!”) return result; }
11
CS 100Lecture 1711 The withdrawal thread class ATM extends Thread { Savings_Account account; ATM(Savings_Account savings) { account = savings; } public void run () { account.withdrawal(300); }
12
CS 100Lecture 1712 main public class ATM_Accounts { public static void main (String[] args) { Savings_Account savings = new Savings_Account(4321, 531); ATM east = new ATM(savings); ATM west = new ATM(savings); west.start(); east.start(); }
13
CS 100Lecture 1713 Some methods in class Thread public static Thread currentThread() public final void suspend() public final void resume() public static void sleep(long msec)...
14
CS 100Lecture 1714 2D Arrays in Java Technically, Java does not support multidimensional arrays, e.g., 2D arrays. In practice, multidimensional arrays are supported because a 1D array can have an array as an element. Recall a 2D array allows us to represent values in two dimensions: 5 4 3 2 1 0 6 8 4
15
CS 100Lecture 1715 Declare and Allocate Declare and allocate two-dimensional array: int [ ] [ ] b; b = new int [3] [2]; Note: b.length is 3 and b[0].length is 2. Assigning to array elements could yield: b[0][0] 5 b[0] b[0][1] 3 b[1][0] 8 b[1] b[1][1] -1 b[2][0] 7 b[2] b[2][1] -5
16
CS 100Lecture 1716 Implement Matlab’s zeros in Java // Assign 0 to all elements of b public static void zeroOut(int b[][]) { for (int r= 0; r != b.length; r= r+1) for (int c= 0; c != b[r].length; c= c+1) b[r][c]= 0; }
17
CS 100Lecture 1717 Pascal’s Triangle 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 int [] [] p; p = new int[7][]; // p.length is 7 p[0] = new int[1];// p[0].length is 1 p[1] = new int[2];// p[1].length is 2 p[2] = new int[3];// p]2].length is 3 Note : Elements of an array can be arrays of different lengths!
18
CS 100Lecture 1718 Compute Pascal’s Triangle // Yield Pascal's triangle with size rows public static int[][] calculatePascal(int size) { int[][]p= new int[size][]; //the triangle for (int r= 0; r != size; r= r+1) { // Allocate row i of triangle --its r+1 values p[r]= new int[r + 1]; // Calculate row r of Pascal's triangle p[r][0]= 1; for (int c= 1; c < r; c= c+1) p[r][c]= p[r-1][c-1] + p[r-1][c]; p[r][r]= 1; } return p; }
19
CS 100Lecture 1719 Magic Squares 1724 1 815 each row sums to 65 23 5 71416 each col sums to 65 4 6132022 each diagonal sums 10121921 3 to 65 11 18 25 2 9 163213In Albert Duerer’s 5 1011 8engraving 9 6 7 12“Melancolia”, done 4 15 14 1in 1514!
20
CS 100Lecture 1720 Construct a Magic Square To construct n x n magic square n x n ( n odd): 0. Put 1 in b[0][n%2] 1. If i is in b[r][c], then i+1 goes in b[r’][c’], where r’,c’ determined as follows: (a) try r’= r-1, if -1, then use r’=n-1. (b) try c’= c+1, if n, then use c’= 0. (c) if b[r’][c’] is already filled, use r’=r+1, c’= c
21
CS 100Lecture 1721 Magic Square pseudo-code static public void magicSquare(int b[][]) { int r= 0; int c= 0; int i; int size= b.length; zeroOut(b); // Make up the magic square r= 0; c= size/2; i= 1; while (b[r][c] == 0) { b[r][c]= i; i= i+1; if (b[mod(r-1, size)][mod(c+1, size)] == 0) { r= mod(r-1, size); c= mod(c+1, size); } else r= mod(r+1,size); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.