Download presentation
Presentation is loading. Please wait.
Published byCameron August Heath Modified over 9 years ago
1
Concurrency Control 1 Fall 2014 CS7020: Game Design and Development
2
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
3
Concurrency vs. Parallelism 3 Fall 2014 CS7020: Game Design and Development CPU CPU1 CPU2
4
Threads and Processes 4 Fall 2014 CS7020: Game Design and Development CPU Process 1 Process 3 Process 2 Process 4 main run GC
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
Interference Between Threads 15 Fall 2014 CS7020: Game Design and Development
16
serialized execution 16 Fall 2014 CS7020: Game Design and Development
17
Interference Between Threads 17 Fall 2014 CS7020: Game Design and Development Example – ThreadAandThreadBinterfering.java
18
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
19
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
20
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
21
Atomic Objects Make the shared variable as atomic 21 Fall 2014 CS7020: Game Design and Development Example – ThreadAandThreadBatomic.java
22
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
23
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
24
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.