addendum to lab 10 what was all that about?. consider… a static queue class – it has one copy of...

15
Addendum to Lab 10 What was all that about?

Upload: willa-powell

Post on 05-Jan-2016

218 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

Addendum to Lab 10

What was all that about?

Page 2: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

Consider…• A static queue class

– It has one copy of the queue in the class’s memory :

• public class StaticQClass{ protected final int DEFCAP = 10; // default capacity private static int[DEFCAP] queue; // array that holds queue elements private static int numElements = 0; // number of elements n the queue private static int front = 0; // index of front of queue private static int rear; // index of rear of queue public StaticQClass() { // details... }

public void enqueue(int item) { // details... }

public void dequeue(int item) { // details... }

Page 3: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

… StaticQClass• If we use the BlueJ interface (and imagination) we can

– Create two StaticQClass objects – (still with one queue shared between them!)

• sqcEnqueuer and sqcDequeuer

• And then enqueue 2 elements with the sqcEnqueuer object “manually”

• sqcEnqueuer.enqueue(5);• sqcEnqueuer.enqueue(27);

• And then dequeue 2 elements with the sqcDequeuer object “manually”

• int first = sqcDequeuer.dequeue(); // just capturing result here• Int second = sqcDequeuer.dequeue(); // still just capturing result

• What if there was a way for us to code this behavior?– To avoid chaos, we should have the ability to coordinate behavior of separate objects

Page 4: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

That’s kinda what threads is about…

• Taken from poster for “Scanners”

Page 5: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

Let’s review the lab demos

And attempt to make sense of it all!

Page 6: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

5.7 Concurrency, Interference, and Synchronization

• Multitask: Perform more than one task at a time

• Concurrency: Several interacting code sequences are executing simultaneously– through interleaving of statements by a single

processor – Or, via several processors (cpu’s) at the same time.

Page 7: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

5.7 Concurrency, Interference, and Synchronization

• Multitask: Perform more than one task at a time

• Concurrency: Several interacting code sequences are executing simultaneously– through interleaving of statements by a single

processor– through execution on several processors

Page 8: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

Counter Class

//-------------------------------------------------------------------------// Counter.java by Dale/Joyce/Weems Chapter 5//// Tracks the current value of a counter.//-------------------------------------------------------------------------package ch05.threads;public class Counter{ private int count; public Counter() { count = 0; }

public void increment() { count++; }

public String toString() { return "Count is:\t" + count; }}

Page 9: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

Demo One - Basicimport ch05.threads.;public class Demo01{

public static void main(String[] args){

Counter myCounter = new Counter();myCounter.increment();myCounter.increment();myCounter.increment();System.out.println(myCounter);

}}

The output of the program is:

Count is: 3

Page 10: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

Demo Two - Threadspackage ch05.threads;public class Increase implements Runnable{ private Counter c; private int amount; public Increase (Counter c, int amount) { this.c = c; this.amount = amount; }

public void run() { for (int i = 1; i <= amount; i++) c.increment(); }}

import ch05.threads.*;public class Demo02{ public static void main(String[] args) throws

InterruptedException5 { Counter c = new Counter(); Runnable r = new Increase(c, 10000); Thread t = new Thread(r); t.start(); System.out.println("Count is: " + c); }}

Output Varies: 86, 66, 44 ????

Page 11: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

Demo Two - Threads

Page 12: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

Demo Three - Join package ch05.threads;public class Increase implements Runnable{ private Counter c; private int amount; public Increase (Counter c, int amount) { this.c = c; this.amount = amount; }

public void run() { for (int i = 1; i <= amount; i++) c.increment(); }}

import ch05.threads.*;public class Demo02{ public static void main(String[] args) throws

InterruptedException5 { Counter c = new Counter(); Runnable r = new Increase(c, 10000); Thread t = new Thread(r); t.start(); t.join(); System.out.println("Count is: " + c); }}

Output is 10000

Page 13: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

Demo Four - Interferencepackage ch05.threads;public class Increase implements Runnable{ private Counter c; private int amount; public Increase (Counter c, int amount) { this.c = c; this.amount = amount; }

public void run() { for (int i = 1; i <= amount; i++) c.increment(); }}

import ch05.threads.*;public class Demo04{ public static void main(String[] args) throws InterruptedException { Counter c = new Counter(); Runnable r1 = new Increase(c, 5000); Runnable r2 = new Increase(c, 5000); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("Count is: " + c); }}

Output Varies: 9861, 9478 ????

Page 14: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

Demo Four - Interference

Thread t1 Thread t2

Step 1: obtains value 12Step 2: obtains value 12

Step 3: increments value to 13 Step 4: stores the value 13

Step 5: increments value to 13Step 6: stores the value 13

Page 15: Addendum to Lab 10 What was all that about?. Consider… A static queue class – It has one copy of the queue in the class’s memory : public class StaticQClass

Demo Five - Synchronization// SyncCounter.java // Tracks the current value of a counter.// Provides synchronized access

package ch05.threads;public class SyncCounter{ private int count; public SyncCounter() { count = 0; }

public synchronized void increment() { count++; }

public String toString() { return "Count is:\t" + count; }}

import ch05.threads.*;public class Demo05{ public static void main(String[] args) throws

InterruptedException { SyncCounter sc = new SyncCounter(); Runnable r1 = new Increase2(sc, 5000); Runnable r2 = new Increase2(sc, 5000); Thread t1 = new Thread(r1); Thread t2 = new Thread(r2); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println("Count is: " + sc); }}

Output is 10000