Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code...
-
Upload
austin-morton -
Category
Documents
-
view
219 -
download
0
description
Transcript of Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code...
![Page 1: Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.](https://reader036.fdocuments.us/reader036/viewer/2022081808/5a4d1b417f8b9ab0599a11e0/html5/thumbnails/1.jpg)
Concurrency (Threads)
Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed
procedurally from start to finish. In a threaded program, you can have multiple
threads working concurrently.
![Page 2: Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.](https://reader036.fdocuments.us/reader036/viewer/2022081808/5a4d1b417f8b9ab0599a11e0/html5/thumbnails/2.jpg)
Concurrency (Threads)
Concurrency is achieved through time-slicing. This is where the processor cycles through each active thread for an indeterminate period of time (the slice). This gives the illusion that there are multiple processes.
With multi-core processors, this may mean, true multi-threading is possible, but NOT guaranteed
![Page 3: Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.](https://reader036.fdocuments.us/reader036/viewer/2022081808/5a4d1b417f8b9ab0599a11e0/html5/thumbnails/3.jpg)
Thread Pools
Thread Pools manage (and limit) the number of active threads. This tends to be more orderly and more efficient for scaled applications.
![Page 4: Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.](https://reader036.fdocuments.us/reader036/viewer/2022081808/5a4d1b417f8b9ab0599a11e0/html5/thumbnails/4.jpg)
Synchronizing methods
When multiple threads have access to the same object, it makes sense to synchronize those methods which are prone to concurrency errors.
The bank account example.
![Page 5: Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.](https://reader036.fdocuments.us/reader036/viewer/2022081808/5a4d1b417f8b9ab0599a11e0/html5/thumbnails/5.jpg)
Thread-safe collections
//http://download.oracle.com/javase/6/docs/api/java/util/concurrent/package-summary.html
![Page 6: Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.](https://reader036.fdocuments.us/reader036/viewer/2022081808/5a4d1b417f8b9ab0599a11e0/html5/thumbnails/6.jpg)
Searching
Linear search O(n) --slowBinary search O(log2n) --fastRefresher on logs:
If 23 = 8 then log28 = 3
Hashed search O(1) –fastest
Search driver class
![Page 7: Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.](https://reader036.fdocuments.us/reader036/viewer/2022081808/5a4d1b417f8b9ab0599a11e0/html5/thumbnails/7.jpg)
![Page 8: Concurrency (Threads) Threads allow you to do tasks in parallel. In an unthreaded program, you code is executed procedurally from start to finish. In a.](https://reader036.fdocuments.us/reader036/viewer/2022081808/5a4d1b417f8b9ab0599a11e0/html5/thumbnails/8.jpg)
Sorting
SelectionSort O(n2) –-slowMergeSort O(n * log2n) –- fast
HeapSort O(n * log2n) –- fast
QuickSort O(n * log2n) –- fast