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

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

Embed Size (px)

Citation preview

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

1

Advanced Computer Programming

Concurrency

Multithreaded Programs

Copyright © Texas Education Agency, 2013

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

Overview

Why Concurrency? Threads Scheduling Runnable interface Race Conditions Locks Thread Safe Dead Lock

2Copyright © Texas Education Agency, 2013

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

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 – Concurrency 33Copyright © Texas Education Agency, 2013

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

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 – Concurrency 44Copyright © Texas Education Agency, 2013

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

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 – Concurrency 55Copyright © Texas Education Agency, 2013

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

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 – Concurrency 66Copyright © Texas Education Agency, 2013

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

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 – Concurrency 77Copyright © Texas Education Agency, 2013

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

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 – Concurrency 88Copyright © Texas Education Agency, 2013

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

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 – Concurrency 99Copyright © Texas Education Agency, 2013

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

Runnable interface

public class Task implements Runnable{ public Task() {} public void run() { // do stuff here }}

IT: Advanced Computer Programming – Concurrency 1010Copyright © Texas Education Agency, 2013

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

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 – Concurrency 1111Copyright © Texas Education Agency, 2013

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

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 – Concurrency 1212Copyright © Texas Education Agency, 2013

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

Locks

public class BankAccount{ //… // synchronized allows deposits to // be entered safely public synchronized deposit(int amount) { balance = balance + amount; } // …}

IT: Advanced Computer Programming – Concurrency 1313Copyright © Texas Education Agency, 2013

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

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 – Concurrency 1414Copyright © Texas Education Agency, 2013

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

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 – Concurrency 1515Copyright © Texas Education Agency, 2013

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

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 – Concurrency 1616Copyright © Texas Education Agency, 2013