Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013.

Similar presentations


Presentation on theme: "1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013."— Presentation transcript:

1 1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013

2 Overview Why Concurrency? Threads Scheduling Runnable interface Race Conditions Locks Thread Safe Dead Lock 2Copyright © Texas Education Agency, 2013

3 Why Concurrency? There are times when you write a program that you want to do more than one thing at a time: Draw to the screen while getting input from the keyboard or mouse Continuously redraw the screen while making calculations (e.g. A progress bar) Have an AI “determine its move” while you are making your move Saving a file while working on a document IT: Advanced Computer Programming – Concurrency33Copyright © Texas Education Agency, 2013

4 Why Concurrency? Java natively supports doing more than one thing at once with a robust concurrency library. Concurrency allows multiple tasks to be accomplished at once using threads of execution. IT: Advanced Computer Programming – Concurrency44Copyright © Texas Education Agency, 2013

5 Threads Threads are the computer processes that execute tasks from beginning to end. If you’ve written a program, you’ve used a thread. The main() method in Java is an example of a thread. It begins execution with the first instruction in main and continues until it reaches the last instruction in the method (or an infinite loop if you accidentally create one). IT: Advanced Computer Programming – Concurrency55Copyright © Texas Education Agency, 2013

6 Scheduling – Single Processor The magic behind concurrency is scheduling. Normally, a computer will determine the details of how a multithreaded program runs. For example, if you only have one processor on a computer, clearly only one process or task can run at a time. What the computer does to overcome this is “time share”, or allow one process to run for a little while, stop, and let another process run for a little while. This continues until all processes cease functioning. IT: Advanced Computer Programming – Concurrency66Copyright © Texas Education Agency, 2013

7 Scheduling – Multicore Some operating systems allow you to take advantage of multicore processors which give programs the opportunity to simultaneously run as many processes as there are cores. This allows multithreaded programs to execute much faster. IT: Advanced Computer Programming – Concurrency77Copyright © Texas Education Agency, 2013

8 Runnable interface In Java, the most basic tasks implement the Runnable interface which is passed to a Thread. The Runnable interface requires that you implement a run() method. The run() method is where you provide the task to be executed in the thread. IT: Advanced Computer Programming – Concurrency88Copyright © Texas Education Agency, 2013

9 Runnable interface In Java, the most basic tasks implement the Runnable interface which is passed to a Thread. The Runnable interface requires that you implement a run() method. The run() method is where you provide the task to be executed in the thread. IT: Advanced Computer Programming – Concurrency99Copyright © Texas Education Agency, 2013

10 Runnable interface public class Task implements Runnable { public Task() {} public void run() { // do stuff here } IT: Advanced Computer Programming – Concurrency10 Copyright © Texas Education Agency, 2013

11 Race Conditions One of the risks of working with multithreaded programs is when two threads try to use variables on the same object at the same time. This is called a race condition. For example, one thread accesses a car object. This thread stops running and a second thread takes over and deletes the car object. The second thread finishes and the first thread continues executing and tries to access a value on the now deleted car. IT: Advanced Computer Programming – Concurrency11 Copyright © Texas Education Agency, 2013

12 Locks To solve race conditions, you can use locks. A lock is a piece of code that prevents other threads from accessing an object until the locking thread is done with it. In Java, this is accomplished with the synchronized keyword. IT: Advanced Computer Programming – Concurrency12 Copyright © Texas Education Agency, 2013

13 Locks public class BankAccount { //… // synchronized allows deposits to // be entered safely public synchronized deposit(int amount) { balance = balance + amount; } // … } IT: Advanced Computer Programming – Concurrency13 Copyright © Texas Education Agency, 2013

14 Thread Safe A function that uses the synchronized keyword is said to be thread safe. There are data structures in Java that are thread safe: Vector, Stack and Hashtable. Other data structures can be made thread safe, examine the Java API documentation for details. IT: Advanced Computer Programming – Concurrency14 Copyright © Texas Education Agency, 2013

15 Dead Lock Multithreaded programs can be very difficult to debug and one of the most difficult problems is dead lock. Dead lock occurs when two functions try to access two different resources. One function will have a lock on one object, the other function will have a lock on the other object -- and they are both waiting for each other to let go of their lock. Often, this can be mistaken as an infinite loop. IT: Advanced Computer Programming – Concurrency15 Copyright © Texas Education Agency, 2013

16 Dead Lock The best way to prevent dead lock is to ensure that you always access objects and resources in the same order. IT: Advanced Computer Programming – Concurrency16 Copyright © Texas Education Agency, 2013


Download ppt "1 Advanced Computer Programming Concurrency Multithreaded Programs Copyright © Texas Education Agency, 2013."

Similar presentations


Ads by Google