Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
-
Upload
cameron-august-heath -
Category
Documents
-
view
229 -
download
0
Transcript of Concurrency Control 1 Fall 2014 CS7020: Game Design and Development.
1
Concurrency Control
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
10Fall 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
12Fall 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
14Fall 2014 CS7020: Game Design and Development
Interference Between Threads
15Fall 2014 CS7020: Game Design and Development
serialized execution
16Fall 2014 CS7020: Game Design and Development
Interference Between Threads
17Fall 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.
18Fall 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.
19Fall 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; } }
20Fall 2014 CS7020: Game Design and Development
Atomic Objects
Make the shared variable as atomic
21Fall 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.
22Fall 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.
23Fall 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.
24Fall 2014 CS7020: Game Design and Development
Example – cp2a.java