Author(s): Javier Fernandez Gonzalez
Edition: 2
Publisher: Packt Publishing
Year: 2017
Language: English
Pages: 697
Preface......Page 28
What this book covers......Page 29
What you need for this book......Page 32
Who this book is for......Page 33
Conventions......Page 34
Reader feedback......Page 35
Customer support......Page 36
Downloading the example code......Page 37
Errata......Page 38
Piracy......Page 39
Questions......Page 40
The First Step - Concurrency Design Principles......Page 41
Basic concurrency concepts......Page 42
Concurrency versus parallelism......Page 43
Synchronization......Page 44
Immutable object......Page 46
Atomic operations and variables......Page 47
Shared memory versus message passing......Page 48
Possible problems in concurrent applications......Page 49
Data race......Page 50
Deadlock......Page 51
Livelock......Page 53
Resource starvation......Page 54
Priority inversion......Page 55
A methodology to design concurrent algorithms......Page 56
The starting point - a sequential version of the algorithm......Page 57
Step 1 - analysis......Page 58
Step 2 - design......Page 59
Step 3 - implementation......Page 60
Step 4 - testing......Page 61
Step 5 - tuning......Page 62
Conclusion......Page 64
Java Concurrency API......Page 65
Basic concurrency classes......Page 66
Synchronization mechanisms......Page 67
Executors......Page 68
The fork/join framework......Page 69
Parallel streams......Page 70
Concurrent data structures......Page 71
Concurrency design patterns......Page 72
Signaling......Page 73
Rendezvous......Page 74
Mutex......Page 75
Multiplex......Page 76
Barrier......Page 77
Double-checked locking......Page 78
Read-write lock......Page 80
Thread pool......Page 81
Thread local storage......Page 82
Tips and tricks for designing concurrent algorithms......Page 83
Identifying the correct independent tasks......Page 84
Implementing concurrency at the highest possible level......Page 85
Taking scalability into account......Page 86
Using thread-safe APIs......Page 87
Never assume an execution order......Page 88
Preferring local thread variables over static and shared when possible......Page 89
Finding the easier parallelizable version of the algorithm......Page 90
Using immutable objects when possible......Page 91
Avoiding deadlocks by ordering the locks......Page 93
Using atomic variables instead of synchronization......Page 94
Holding locks for as short a time as possible......Page 95
Taking precautions using lazy initialization......Page 96
Avoiding the use of blocking operations inside a critical section......Page 97
Summary......Page 98
Working with Basic Elements - Threads and Runnables......Page 99
Threads in Java......Page 100
Threads in Java - characteristics and states......Page 102
The Thread class and the Runnable interface......Page 104
First example: matrix multiplication......Page 106
Common classes......Page 107
Serial version......Page 108
Parallel versions......Page 110
First concurrent version - a thread per element......Page 111
Second concurrent version - a thread per row......Page 114
Third concurrent version - the number of threads is determined by the processors......Page 116
Comparing the solutions......Page 119
Second example - file search......Page 122
Common classes......Page 123
Serial version......Page 124
Concurrent version......Page 125
Comparing the solutions......Page 130
Summary......Page 132
Managing Lots of Threads - Executors......Page 133
An introduction to executors......Page 134
Basic characteristics of executors......Page 135
Basic components of the Executor framework......Page 136
First example - the k-nearest neighbors algorithm......Page 137
k-nearest neighbors - serial version......Page 139
K-nearest neighbors - a fine-grained concurrent version......Page 141
k-nearest neighbors - a coarse-grained concurrent version......Page 145
Comparing the solutions......Page 147
Second example - concurrency in a client/server environment......Page 150
Client/server - serial version......Page 152
The DAO part......Page 153
The command part......Page 154
The server part......Page 156
Client/version - parallel version......Page 158
The server part......Page 159
The command part......Page 163
Extra components of the concurrent server......Page 164
The status command......Page 165
The cache system......Page 167
The log system......Page 170
Comparing the two solutions......Page 173
Other methods of interest......Page 175
Summary......Page 177
Getting the Most from Executors......Page 178
Advanced characteristics of executors......Page 179
Cancellation of tasks......Page 180
Scheduling the execution of tasks......Page 181
Overriding the executor methods......Page 182
Changing some initialization parameters......Page 183
First example - an advanced server application......Page 184
The ServerExecutor class......Page 186
The statistics object......Page 187
The rejected task controller......Page 188
The executor tasks......Page 189
The executor......Page 190
The command classes......Page 193
The ConcurrentCommand class......Page 194
The concrete commands......Page 196
The server part......Page 198
The ConcurrentServer class......Page 199
The RequestTask class......Page 203
The client part......Page 206
Second example - executing periodic tasks......Page 207
The common parts......Page 208
The basic reader......Page 211
The advanced reader......Page 215
Additional information about executors......Page 219
Summary......Page 220
Getting Data from Tasks - The Callable and Future Interfaces......Page 221
Introducing the Callable and Future interfaces......Page 222
The Callable interface......Page 223
The Future interface......Page 224
First example - a best-matching algorithm for words......Page 225
The common classes......Page 226
A best-matching algorithm - the serial version......Page 228
The BestMatchingSerialCalculation class......Page 229
The BestMachingSerialMain class......Page 230
A best-matching algorithm - the first concurrent version......Page 231
The BestMatchingBasicTask class......Page 232
The BestMatchingBasicConcurrentCalculation class......Page 234
A best-matching algorithm - the second concurrent version......Page 237
Word exists algorithm - a serial version......Page 239
The ExistSerialCalculation class......Page 240
The ExistSerialMain class......Page 241
Word exists algorithm - the concurrent version......Page 242
The ExistBasicTasks class......Page 243
The ExistBasicConcurrentCalculation class......Page 245
The ExistBasicConcurrentMain class......Page 247
Comparing the solutions......Page 248
Best-matching algorithms......Page 249
Exist algorithms......Page 251
The second example - creating an inverted index for a collection of documents......Page 253
Common classes......Page 255
The Document class......Page 256
The DocumentParser class......Page 257
The serial version......Page 258
The first concurrent version - a task per document......Page 260
The IndexingTask class......Page 262
The InvertedIndexTask class......Page 263
The ConcurrentIndexing class......Page 265
The second concurrent version - multiple documents per task......Page 268
The MultipleIndexingTask class......Page 269
The MultipleInvertedIndexTask class......Page 270
The MultipleConcurrentIndexing class......Page 271
Comparing the solutions......Page 272
Other methods of interest......Page 274
Summary......Page 275
Running Tasks Divided into Phases - The Phaser Class......Page 277
An introduction to the Phaser class......Page 278
Registration and deregistration of participants......Page 279
Synchronizing phase change......Page 280
Other functionalities......Page 281
First example - a keyword extraction algorithm......Page 283
Common classes......Page 285
The Word class......Page 286
The Keyword class......Page 288
The Document class......Page 289
The DocumentParser class......Page 290
The serial version......Page 292
The concurrent version......Page 296
The KeywordExtractionTask class......Page 297
The ConcurrentKeywordExtraction class......Page 301
Comparing the two solutions......Page 304
The second example - a genetic algorithm......Page 306
Common classes......Page 309
The Individual class......Page 310
The GeneticOperators class......Page 311
The serial version......Page 313
The SerialGeneticAlgorithm class......Page 314
The SerialMain class......Page 316
The concurrent version......Page 317
The SharedData class......Page 318
The GeneticPhaser class......Page 319
The ConcurrentGeneticTask class......Page 321
The ConcurrentGeneticAlgorithm class......Page 324
The ConcurrentMain class......Page 326
Comparing the two solutions......Page 327
Lau15 dataset......Page 328
Kn57 dataset......Page 329
Conclusions......Page 330
Summary......Page 331
Optimizing Divide and Conquer Solutions - The Fork/Join Framework......Page 333
An introduction to the fork/join framework......Page 334
Basic characteristics of the fork/join framework......Page 336
Limitations of the fork/join framework......Page 338
Components of the fork/join framework......Page 339
The first example - the k-means clustering algorithm......Page 340
The common classes......Page 343
The VocabularyLoader class......Page 344
The word, document, and DocumentLoader classes......Page 345
The DistanceMeasurer class......Page 347
The DocumentCluster class......Page 348
The serial version......Page 349
The SerialKMeans class......Page 350
The SerialMain class......Page 352
The concurrent version......Page 353
Two tasks for the fork/join framework - AssignmentTask and UpdateTask......Page 354
The ConcurrentKMeans class......Page 357
The ConcurrentMain class......Page 360
Comparing the solutions......Page 361
The second example - a data filtering algorithm......Page 364
Common features......Page 365
The serial version......Page 366
The SerialSearch class......Page 367
The SerialMain class......Page 368
The concurrent version......Page 370
The TaskManager class......Page 371
The IndividualTask class......Page 373
The ListTask class......Page 376
The ConcurrentSearch class......Page 378
The ConcurrentMain class......Page 379
Comparing the two versions......Page 380
The third example - the merge sort algorithm......Page 383
Shared classes......Page 384
The serial version......Page 385
The SerialMergeSort class......Page 386
The SerialMetaData class......Page 388
The concurrent version......Page 389
The MergeSortTask class......Page 390
The ConcurrentMergeSort class......Page 392
The ConcurrentMetaData class......Page 393
Comparing the two versions......Page 394
Other methods of the fork/join framework......Page 396
Summary......Page 397
Processing Massive Datasets with Parallel Streams - The Map and Reduce Model......Page 399
An introduction to streams......Page 400
Basic characteristics of streams......Page 401
Sections of a stream......Page 403
Sources of a stream......Page 404
Intermediate operations......Page 406
Terminal operations......Page 407
MapReduce versus MapCollect......Page 408
The first example - a numerical summarization application......Page 409
The concurrent version......Page 410
The ConcurrentDataLoader class......Page 411
The ConcurrentStatistics class......Page 413
Customers from the United Kingdom......Page 414
Quantity from the United Kingdom......Page 415
Countries for product......Page 416
Quantity for product......Page 417
Multiple data filter......Page 419
Highest invoice amounts......Page 421
Products with a unit price between 1 and 10......Page 423
The ConcurrentMain class......Page 424
The serial version......Page 426
Comparing the two versions......Page 427
The second example - an information retrieval search tool......Page 429
An introduction to the reduction operation......Page 430
The first approach - full document query......Page 432
The basicMapper() method......Page 434
The Token class......Page 435
The QueryResult class......Page 436
The second approach - reduced document query......Page 438
The limitedMapper() method......Page 439
The third approach - generating an HTML file with the results......Page 440
The ContentMapper class......Page 442
The fourth approach - preloading the inverted index......Page 444
The ConcurrentFileLoader class......Page 446
The fifth approach - using our own executor......Page 447
Getting data from the inverted index - the ConcurrentData class......Page 448
Getting the number of words in a file......Page 449
Getting the average tfxidf value in a file......Page 450
Getting the maximum and minimum tfxidf values in the index......Page 451
The ConcurrentMain class......Page 452
The serial version......Page 453
Comparing the solutions......Page 454
Summary......Page 458
Processing Massive Datasets with Parallel Streams - The Map and Collect Model......Page 459
Using streams to collect data......Page 460
The collect() method......Page 461
The first example - searching data without an index......Page 464
Basic classes......Page 465
The Product class......Page 466
The Review class......Page 467
The ProductLoader class......Page 468
The first approach - basic search......Page 469
The ConcurrentStringAccumulator class......Page 471
The second approach - advanced search......Page 473
The ConcurrentObjectAccumulator class......Page 475
A serial implementation of the example......Page 476
Comparing the implementations......Page 477
The second example - a recommendation system......Page 479
Common classes......Page 480
The ProductReview class......Page 481
The ProductRecommendation class......Page 482
Recommendation system - the main class......Page 483
The ConcurrentLoaderAccumulator class......Page 487
The serial version......Page 488
Comparing the two versions......Page 489
The third example - common contacts in a social network......Page 491
Base classes......Page 493
The Person class......Page 494
The PersonPair class......Page 495
The DataLoader class......Page 496
The concurrent version......Page 497
The CommonPersonMapper class......Page 498
The ConcurrentSocialNetwork class......Page 500
The ConcurrentMain class......Page 504
The serial version......Page 505
Comparing the two versions......Page 506
Summary......Page 508
Asynchronous Stream Processing - Reactive Streams......Page 509
Introduction to reactive streams in Java......Page 511
The Flow.Publisher interface......Page 512
The Flow.Subscriber interface......Page 513
The Flow.Subscription interface......Page 514
The SubmissionPublisher class......Page 515
The first example - a centralized system for event notification......Page 516
The Event class......Page 517
The Producer class......Page 518
The Consumer class......Page 519
The Main class......Page 521
The second example - a news system......Page 524
The News class......Page 525
The publisher classes......Page 526
The Consumer class......Page 531
The Main class......Page 533
Summary......Page 536
Diving into Concurrent Data Structures and Synchronization Utilities......Page 537
Concurrent data structures......Page 538
Blocking and non-blocking data structures......Page 539
Concurrent data structures......Page 540
Interfaces......Page 541
BlockingQueue......Page 542
BlockingDeque......Page 544
ConcurrentMap......Page 545
TransferQueue......Page 546
Classes......Page 547
LinkedBlockingQueue......Page 548
ConcurrentLinkedQueue......Page 549
LinkedBlockingDeque......Page 550
ConcurrentLinkedDeque......Page 551
ArrayBlockingQueue......Page 552
DelayQueue......Page 553
LinkedTransferQueue......Page 554
PriorityBlockingQueue......Page 555
ConcurrentHashMap......Page 556
Using the new features......Page 557
First example with ConcurrentHashMap......Page 558
The forEach() method......Page 559
The search() method......Page 561
The reduce() method......Page 563
The compute() method......Page 565
Another example with ConcurrentHashMap......Page 566
An example with the ConcurrentLinkedDeque class......Page 568
The removeIf() method......Page 569
The spliterator() method......Page 570
Atomic variables......Page 573
Variable handles......Page 575
Synchronization mechanisms......Page 578
The CommonTask class......Page 580
The Lock interface......Page 581
The Semaphore class......Page 583
The CountDownLatch class......Page 585
The CyclicBarrier class......Page 587
The CompletableFuture class......Page 589
Using the CompletableFuture class......Page 592
Auxiliary tasks......Page 593
The main() method......Page 595
Summary......Page 599
Testing and Monitoring Concurrent Applications......Page 600
Monitoring concurrency objects......Page 601
Monitoring a thread......Page 602
Monitoring a lock......Page 604
Monitoring an executor......Page 606
Monitoring the fork/join framework......Page 608
Monitoring a Phaser......Page 610
Monitoring the Stream API......Page 611
Monitoring concurrency applications......Page 612
The Overview tab......Page 615
The Memory tab......Page 617
The Threads tab......Page 619
The Classes tab......Page 620
The VM summary tab......Page 622
The MBeans tab......Page 624
The About tab......Page 626
Testing concurrency applications......Page 627
Testing concurrent applications with MultithreadedTC......Page 628
Testing concurrent applications with Java Pathfinder......Page 632
Installing Java Pathfinder......Page 633
Running Java Pathfinder......Page 636
Summary......Page 640
Concurrency in JVM - Clojure and Groovy with the Gpars Library and Scala......Page 641
Concurrency in Clojure......Page 642
Using Java elements......Page 643
Reference types......Page 644
Atoms......Page 645
Agents......Page 647
Refs......Page 649
Delays......Page 651
Futures......Page 653
Promises......Page 655
Concurrency in Groovy with the GPars library......Page 657
Software transactional memory......Page 658
Using Java elements......Page 659
Data parallelism......Page 660
The fork/join processing......Page 665
Actors......Page 668
Agent......Page 676
Dataflow......Page 678
Concurrency in Scala......Page 686
Future objects in Scala......Page 687
Promises......Page 695
Summary......Page 697