Concurrency Control 1 Fall 2014 CS7020: Game Design and Development Multitasking and Multithreading Multitasking: – – Multithreading: – – – 2 refers to a computer's ability to perform multiple jobs concurrently more than one program are running concurrently, e.g., UNIX 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 Fall 2014 CS7020: Game Design and Development Concurrency vs. Parallelism CPU 3 Fall 2014 CS7020: Game Design and Development CPU1 CPU2 Threads and Processes CPU main run Process 1 Process 2 Process GC 4 Fall 2014 CS7020: Game Design and Development 3 Process 4 Application Thread 5 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 Fall 2014 CS7020: Game Design and Development Multiple Threads in an Application 6 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 Fall 2014 CS7020: Game Design and Development Creating Threads 7 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 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() – – void run() – The new thread begins its life inside this method void stop() (deprecated) – 9 Creates a new thread and makes it runnable This method can be called only once The thread is being terminated 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) – 10 The thread sleeps for m milliseconds, plus n nanoseconds 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 12 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 Fall 2014 CS7020: Game Design and Development Example – UseRunnableInThread.java Thread State Diagram Alive new New Thread(); Thread Running while (…) { … } Runnable thread.start(); Blocked Dead run() Thread method returns Object.wait() Thread.sleep() blocking 13 Fall 2014 CS7020: Game Design and Development IO call waiting on a monitor Race Condition 14 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 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 Example – 17 Fall 2014 CS7020: Game Design and Development ThreadAandThreadBinterfering.java Deadlock 18 .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. Fall 2014 CS7020: Game Design and Development Synchronization in Java 19 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. 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 Example – ThreadAandThreadBatomic.java 21 Fall 2014 CS7020: Game Design and Development 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. Example – cp0.java; cp1.java 22 Fall 2014 CS7020: Game Design and Development 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. Example – cp2.java 23 Fall 2014 CS7020: Game Design and Development 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. Example – 24 Fall 2014 CS7020: Game Design and Development cp2a.java