300 likes | 490 Views
Concurrency Control. 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
E N D
Concurrency Control Fall 2013 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 Fall 2013 CS7020: Game Design and Development
Concurrency vs. Parallelism CPU CPU1 CPU2 Fall 2013 CS7020: Game Design and Development
Threads and Processes CPU Process 1 Process 2 Process 3 Process 4 main run GC Fall 2013 CS7020: Game Design and Development
Application Thread • When we execute an application: • The JVM createsa Thread object whose task is defined by themain() method • The JVM startsthe thread • The threadexecutes the statements of the program one by one • After executing all the statements, the method returns and the thread dies Fall 2013 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 (are there variables on the heap?) • Two different threads can act on the same object and same static fields concurrently Fall 2013 CS7020: Game Design and Development
Creating Threads • There are two ways to create our own Thread object • Extend the Thread class and instantiating a new object of that class • Implementing the Runnable interface • In both cases the run() method should be implemented Fall 2013 CS7020: Game Design and Development
Extending Thread public class ThreadExampleextends Thread { public void run () { for (inti = 1; i <= 100; i++) { System.out.println(“---”); } } } Fall 2013 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 Fall 2013 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) orsleep(int m, intn) • The thread sleeps for m milliseconds, plus n nanoseconds Fall 2013 CS7020: Game Design and Development
Implementing Runnable public class RunnableExampleimplements Runnable{ public void run () { for (inti = 1; i <= 100; i++) { System.out.println (“***”); } } } Fall 2013 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 Example – UseRunnableInThread.java Fall 2013 CS7020: Game Design and Development
Thread State Diagram Alive Running new Thread(); while (…) { … } New Thread Runnable Dead Thread thread.start(); run() method returns Blocked Object.wait() Thread.sleep() blocking IO call waiting on a monitor
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 Fall 2013 CS7020: Game Design and Development
Interference Between Threads Fall 2013 CS7020: Game Design and Development
serialized execution Fall 2013 CS7020: Game Design and Development
Interference Between Threads Example – ThreadAandThreadBinterfering.java Fall 2013 CS7020: Game Design and Development
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. Fall 2013 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. Fall 2013 CS7020: Game Design and Development
Example public class BankAccount { private float balance; publicsynchronizedvoid deposit(float amount){ balance += amount; } public synchronizedvoid withdraw(float amount){ balance -= amount; } } Fall 2013 CS7020: Game Design and Development
Atomic Objects • Make the shared variable as atomic Example – ThreadAandThreadBatomic.java Fall 2013 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 Fall 2013 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 ArrayBlockingQueueimplements the interface BlockingQueue. Example – cp2.java Fall 2013 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. • inthas been changed to long here in order to accommodate the large size of the total. Example – cp2a.java Fall 2013 CS7020: Game Design and Development