1 / 24

Concurrency Control

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

jalene
Télécharger la présentation

Concurrency Control

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Concurrency Control Fall 2013 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 Fall 2013 CS7020: Game Design and Development

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

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

  5. 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

  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 (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

  7. 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

  8. 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

  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 Fall 2013 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) orsleep(int m, intn) • The thread sleeps for m milliseconds, plus n nanoseconds Fall 2013 CS7020: Game Design and Development

  11. 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

  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 Example – UseRunnableInThread.java Fall 2013 CS7020: Game Design and Development

  13. 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

  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 Fall 2013 CS7020: Game Design and Development

  15. Interference Between Threads Fall 2013 CS7020: Game Design and Development

  16. serialized execution Fall 2013 CS7020: Game Design and Development

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

  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. Fall 2013 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. Fall 2013 CS7020: Game Design and Development

  20. 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

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

  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. Example – cp0.java; cp1.java Fall 2013 CS7020: Game Design and Development

  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 ArrayBlockingQueueimplements the interface BlockingQueue. Example – cp2.java Fall 2013 CS7020: Game Design and Development

  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. • 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

More Related