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.

Slides:



Advertisements
Similar presentations
The Queue ADT Definition A queue is a restricted list, where all additions occur at one end, the rear, and all removals occur at the other end, the front.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 18: Stacks And Queues.
Threads We think of a process (a running program) as a single entity Our modern operating systems can run multiple concurrent processes, switching off.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
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.
Threading Part 2 CS221 – 4/22/09. Where We Left Off Simple Threads Program: – Start a worker thread from the Main thread – Worker thread prints messages.
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Synchronization in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Chapter 14 Queues. First a Review Queue processing Using queues to solve problems – Optimizing customer service simulation – Ceasar ciphers – Palindrome.
Tutorial 2 Adventures in Threading presented by: Antonio Maiorano Paul Di Marco.
Multithreading.
Threading in Java – a Tutorial QMUL IEEE SB. Why Threading When we need to run two tasks concurrently So multiple parts (>=2) of a program can run simultaneously.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
1 Java Threads Instructor: Mainak Chaudhuri
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
1 Lecture 12 More on Hashing Multithreading. 2 Speakers!
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.
Threads.
Multithreading in Java Sameer Singh Chauhan Lecturer, I. T. Dept., SVIT, Vasad.
Concurrency in Java Brad Vander Zanden. Processes and Threads Process: A self-contained execution environment Thread: Exists within a process and shares.
Introduction to Threads Session 01 Java Simplified / Session 14 / 2 of 28 Objectives Define a thread Define multithreading List benefits of multithreading.
1 5.0 Garbage collector & Threads : Overview Introduction: In this module we discuss the merits and demerits of automated garbage collection over manual.
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
Concurrent Computing CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
Software and Threading Geza Kovacs Maslab Abstract Design: State Machines By using state machine diagrams, you can find flaws in your behavior without.
System Programming Practical Session 4: Concurrency / Safety.
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.
CMSC 330: Organization of Programming Languages Threads.
Threads in Java Threads Introduction: After completing this chapter, you will be able to code your own thread, control them efficiently without.
Chapter 5 The Queue ADT. Chapter 5: The Queue ADT 5.1 – Queues 5.2 – Formal Specification 5.3 – Array-Based Implementations 5.4 – Application: Palindromes.
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.
Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Chapter 5 The Queue ADT. 5.1 Queues Queue A structure in which elements are added to the rear and removed from the front; a “first in, first out” (FIFO)
Slides by Evan Gallagher
Slides by Evan Gallagher
Multithreading Lec 23.
Multithreading.
Java Concurrency.
Section 5.7 Concurrency, Interference, and Synchronization.
Threads Chate Patanothai.
Multithreaded Programming in Java
Realizing Concurrency using Posix Threads (pthreads)
Condition Variables and Producer/Consumer
CS18000: Problem Solving and Object-Oriented Programming
Threads and Concurrency in Java: Part 1
Condition Variables and Producer/Consumer
Multithreading 2 Lec 24.
Multithreading.
Concurrency in Java Last Updated: Fall 2010 Paul Ammann SWE 619.
IMPLEMENTATION OF A NON-BLOCKING QUEUE ALGORITHM
9. Threads SE2811 Software Component Design
Visit for more Learning Resources
Threads in Java James Brucker.
Multithreading in java.
Threads and Multithreading
Representation and Management of Data on the Internet
9. Threads SE2811 Software Component Design
Chapter 4 The Queue ADT.
Lecture 19 Threads CSE /6/2019.
Threads CSE451 Andrew Whitaker TODO: print handouts for AspectRatio.
9. Threads SE2811 Software Component Design
The Queue ADT Definition A queue is a restricted list, where all additions occur at one end, the rear, and all removals occur at the other end, the front.
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Threads and concurrency / Safety
Presentation transcript:

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 { 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... }

… 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

That’s kinda what threads is about… Taken from poster for “Scanners”

Let’s review the lab demos And attempt to make sense of it all!

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.

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

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; }

Demo One - Basic import ch05.threads.; public class Demo01 { public static void main(String[] args) { Counter myCounter = new Counter(); myCounter.increment(); System.out.println(myCounter); } The output of the program is: Count is: 3

Demo Two - Threads 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(); System.out.println("Count is: " + c); } Output Varies: 86, 66, 44 ????

Demo Two - Threads

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

Demo Four - Interference 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 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 ????

Demo Four - Interference Thread t1 Thread t2 Step 1: obtains value 12 Step 2: obtains value 12 Step 3: increments value to 13 Step 4: stores the value 13 Step 5: increments value to 13 Step 6: stores the value 13

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