Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming.

Similar presentations


Presentation on theme: "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming."— Presentation transcript:

1 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming

2 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Thread Basics Threading allows multiple activities to coexist within a single process Threads are like processes –Independent, concurrent paths of execution Threads are less insulated –Share memory, file handles, preprocess state

3 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Why use threads? Make the UI more responsive Take advantage of multiprocessor systems Simplify modeling Perform asynchronous or background processing

4 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Thread’s Life – Creating Threads Every Java program contains the main thread Additional threads are created through the Thread constructor

5 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Thread’s Life – Starting a Thread Different than creating a thread Must use start() method

6 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Thread’s Life – Ending Threads Three ways: 1.End of the run( ) method 2.Throws an Exception or Error that is not caught 3.Another thread calls the stop( ) method

7 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Joining Threads join( ) method –Waits for the thread to complete –Use when partitioning a larger problem into smaller ones

8 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Scheduling Timing of thread scheduling and execution is nondeterministic Use synchronization to ensure data consistency Synchronization example

9 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sleeping sleep( ) method –Cause the current thread to wait until the specified time has elapsed (in milliseconds) or until the thread is interrupted by another thread –When finished waiting, thread goes back onto scheduler’s queue of runnable threads

10 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Example: Partitioning a large task with multiple threads Use threads to help in a searching algorithm. In-class Example

11 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Sharing Access to Data Shared variables between threads requires synchronization to prevent inconsistent data –Polling –Synchronization Logger Example –Semaphores

12 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Basic rule for synchronizing You must synchronize whenever you are: –Reading a variable that may have been last written by another thread –Writing a variable that may be read next by another thread –Modifying multiple related values, you want other threads to see as atomic changes

13 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Deadlock Occurs when each thread is waiting for an action that one of the others can perform Thread 1 holds a lock on Object A and is waiting for the lock on Object B & Thread 2 holds the lock on Object B and is waiting for the lock on Object A

14 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Semaphores Control the number of activities that can access certain resources Used to … –Implement resource pools –Impose a bound on a collection

15 Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Semaphores Manages a set of permits Initial number passed to constructor Activities acquire and release permits If no permit is available, acquire blocks until one is.


Download ppt "Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Concurrency & Dynamic Programming."

Similar presentations


Ads by Google