Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.

Slides:



Advertisements
Similar presentations
Concurrency Important and difficult (Ada slides copied from Ed Schonberg)
Advertisements

1 CSC321 §2 Concurrent Programming Abstraction & Java Threads Section 2 Concurrent Programming Abstraction & Java Threads.
Java How to Program, 9/e CET 3640 Professor: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
Algorithm Programming Concurrent Programming in Java Bar-Ilan University תשס"ח Moshe Fresko.
Intro to Threading CS221 – 4/20/09. What we’ll cover today Finish the DOTS program Introduction to threads and multi-threading.
Multithreading The objectives of this chapter are:
CS220 Software Development Lecture: Multi-threading A. O’Riordan, 2009.
Definitions Process – An executing program
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Concurrency - 1 Tasking Concurrent Programming Declaration, creation, activation, termination Synchronization and communication Time and delays conditional.
Java How to Program, 9/e CET 3640 Professor: Dr. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
A Bridge to Your First Computer Science Course Prof. H.E. Dunsmore Concurrent Programming Threads Synchronization.
University of Sunderland Java Threading, Mutex and Synchronisation Lecture 02 COMM86 Concurrent and Distributed Software Systems.
Threads. Overview Problem Multiple tasks for computer Draw & display images on screen Check keyboard & mouse input Send & receive data on network Read.
Threads. Java Threads A thread is not an object A thread is a flow of control A thread is a series of executed statements A thread is a nested sequence.
Threads in Java. History  Process is a program in execution  Has stack/heap memory  Has a program counter  Multiuser operating systems since the sixties.
Threading and Concurrency Issues ● Creating Threads ● In Java ● Subclassing Thread ● Implementing Runnable ● Synchronization ● Immutable ● Synchronized.
Lecture 5 : JAVA Thread Programming Courtesy : MIT Prof. Amarasinghe and Dr. Rabbah’s course note.
Multi-Threaded Application CSNB534 Asma Shakil. Overview Software applications employ a strategy called multi- threaded programming to split tasks into.
Quick overview of threads in Java Babak Esfandiari (extracted from Qusay Mahmoud’s slides)
Multithreading in Java Project of COCS 513 By Wei Li December, 2000.
Java Threads Representation and Management of Data on the Internet.
111 © 2002, Cisco Systems, Inc. All rights reserved.
Concurrent Programming. Concurrency  Concurrency means for a program to have multiple paths of execution running at (almost) the same time. Examples:
1 CMSC 341: Data Structures Nilanjan Banerjee Data Structures University of Maryland Baltimore County
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.
C20: Threads see also: ThreadedBallWorld, DropTest, Tetris source examples Not covered: advanced stuff like notify/notifyAll.
1 Object Oriented Programming Lecture XII Multithreading in Java, A few words about AWT and Swing, The composite design pattern.
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
15.1 Threads and Multi- threading Understanding threads and multi-threading In general, modern computers perform one task at a time It is often.
SurfaceView.
Multi-Threading in Java
Peyman Dodangeh Sharif University of Technology Fall 2014.
Java Threads DBI – Representation and Management of Data on the Internet.
Multithreading & Synchronized Algoritma Pemrograman 3 Sistem Komputer – S1 Universitas Gunadarma 1.
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.
L6: Threads “the future is parallel” COMP206, Geoff Holmes and Bernhard Pfahringer.
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.
1 Threads in Java Jingdi Wang. 2 Introduction A thread is a single sequence of execution within a program Multithreading involves multiple threads of.
Concurrency in Java MD. ANISUR RAHMAN. slide 2 Concurrency  Multiprogramming  Single processor runs several programs at the same time  Each program.
Concurrent Programming in Java Based on Notes by J. Johns (based on Java in a Nutshell, Learning Java) Also Java Tutorial, Concurrent Programming in Java.
CS203 Programming with Data Structures Introduction to Threads and Synchronization California State University, Los Angeles.
Multithreading The objectives of this chapter are:
Doing Several Things at Once
Multithreading / Concurrency
Multi Threading.
Java Multithreading.
CSE 501N Fall ‘09 21: Introduction to Multithreading
Multithreaded Programming in Java
Definitions Concurrent program – Program that executes multiple instructions at the same time. Process – An executing program (the running JVM for Java.
Advanced Programming Behnam Hatami Fall 2017.
Multithreading Chapter 23.
Advanced Programming in Java
Multithreading.
Multithreading.
برنامه‌نویسی چندنخی Multi-Thread Programming
Dr. Mustafa Cem Kasapbaşı
Computer Science 2 06A-Java Multithreading
Multithreading in java.
Threads and Multithreading
Representation and Management of Data on the Internet
Lecture 19 Threads CSE /6/2019.
Multithreading The objectives of this chapter are:
Java Chapter 3 (Estifanos Tilahun Mihret--Tech with Estif)
Presentation transcript:

Concurrency Control 1 Fall 2014 CS7020: Game Design and Development

Multitasking and Multithreading  Multitasking: – refers to a computer's ability to perform multiple jobs concurrently – more than one program are running concurrently, e.g., UNIX  Multithreading: – A thread is a single sequence of execution within a program – refers to multiple threads of control within a single program – each program can run multiple threads of control within it, e.g., Web Browser 2 Fall 2014 CS7020: Game Design and Development

Concurrency vs. Parallelism 3 Fall 2014 CS7020: Game Design and Development  CPU  CPU1  CPU2

Threads and Processes 4 Fall 2014 CS7020: Game Design and Development  CPU  Process 1  Process 3  Process 2  Process 4  main  run  GC

Application Thread  When we execute an application: 1. The JVM creates a Thread object whose task is defined by the main() method 2. The JVM starts the thread 3. The thread executes the statements of the program one by one 4. After executing all the statements, the method returns and the thread dies 5 Fall 2014 CS7020: Game Design and Development

Multiple Threads in an Application  Each thread has its private run-time stack  If two threads execute the same method, each will have its own copy of the local variables the methods uses  However, all threads see the same dynamic memory, i.e., heap  Two different threads can act on the same object and same static fields concurrently 6 Fall 2014 CS7020: Game Design and Development

Creating Threads  There are two ways to create our own Thread object 1. Extend the Thread class and instantiating a new object of that class 2. Implementing the Runnable interface  In both cases the run() method should be implemented 7 Fall 2014 CS7020: Game Design and Development

Extending Thread public class ThreadExample extends Thread { public void run () { for (int i = 1; i <= 100; i++) { System.out.println(“---”); } 8 Fall 2014 CS7020: Game Design and Development

Thread Methods  void start() – Creates a new thread and makes it runnable – This method can be called only once  void run() – The new thread begins its life inside this method  void stop() (deprecated) – The thread is being terminated 9 Fall 2014 CS7020: Game Design and Development

Thread Methods  void yield() – Causes the currently executing thread object to temporarily pause and allow other threads to execute – Allow only threads of the same priority to run  void sleep(int m) or sleep(int m, int n) – The thread sleeps for m milliseconds, plus n nanoseconds 10 Fall 2014 CS7020: Game Design and Development

Implementing Runnable public class RunnableExample implements Runnable { public void run () { for (int i = 1; i <= 100; i++) { System.out.println (“***”); } 11 Fall 2014 CS7020: Game Design and Development

A Runnable Object  When running the Runnable object, a Thread object is created from the Runnable object  The Thread object ’ s run() method calls the Runnable object ’ s run() method  Allows threads to run inside any object, regardless of inheritance 12 Fall 2014 CS7020: Game Design and Development Example – UseRunnableInThread.java

Thread State Diagram 13  Alive  New Thread  Dead Thread  Running  Runnable  new Thread();  run() method returns  while (…) { … }  Blocked  Object.wait()  Thread.sleep()  blocking IO call  waiting on a monitor  thread.start(); Fall 2014 CS7020: Game Design and Development

Race Condition  A race condition – the outcome of a program is affected by the order in which the program's threads are allocated CPU time  Two threads are simultaneously modifying a single object  Both threads “race” to store their value 14 Fall 2014 CS7020: Game Design and Development

Interference Between Threads 15 Fall 2014 CS7020: Game Design and Development

serialized execution 16 Fall 2014 CS7020: Game Design and Development

Interference Between Threads 17 Fall 2014 CS7020: Game Design and Development Example – ThreadAandThreadBinterfering.java

Deadlock .One simple case is two processes or threads waiting on each other to do something. Since neither can proceed until the other does, they are stuck. 18 Fall 2014 CS7020: Game Design and Development

Synchronization in Java  The synchronized methods define critical sections  Execution of critical sections is mutually exclusive.  When a method is declared to be synchronized, only one thread is allowed to execute it at a time. 19 Fall 2014 CS7020: Game Design and Development

Example public class BankAccount { private float balance; public synchronized void deposit(float amount){ balance += amount; } public synchronized void withdraw(float amount){ balance -= amount; } 20 Fall 2014 CS7020: Game Design and Development

Atomic Objects  Make the shared variable as atomic 21 Fall 2014 CS7020: Game Design and Development Example – ThreadAandThreadBatomic.java

Producer-Consumer Program  one thread is creating objects and placing them in a shared variable. This thread is the producer. Another thread, the consumer, takes the objects from the shared variable and processes them.  The synchronization problem arises in various ways. 22 Fall 2014 CS7020: Game Design and Development Example – cp0.java; cp1.java

Blocking Queues  Synchronizing the producer and consumer is a little more difficult than the conflicting updates we just studied.  Luckily, the Java library provides a helpful class ArrayBlockingQueue implements the interface BlockingQueue.ArrayBlockingQueueBlockingQueue 23 Fall 2014 CS7020: Game Design and Development Example – cp2.java

Blocking Queues  Here is another version in which the consumer computes a running total of the values. The consumer keeps its own total of the values received, and prints that. The main program prints the total and the expected value of the total.  int has been changed to long here in order to accommodate the large size of the total. 24 Fall 2014 CS7020: Game Design and Development Example – cp2a.java