concurrency (threads) threads allow you to do tasks in parallel. in an unthreaded program, you code...

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 threaded program, you can have multiple threads working concurrently.

Upload: austin-morton

Post on 18-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

Thread Pools Thread Pools manage (and limit) the number of active threads. This tends to be more orderly and more efficient for scaled applications.

TRANSCRIPT

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

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

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

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

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

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

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

Sorting

SelectionSort O(n2) –-slowMergeSort O(n * log2n) –- fast

HeapSort O(n * log2n) –- fast

QuickSort O(n * log2n) –- fast