Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo

1

Concurrency 
in practice 
Mikalai Alimenkou 
http://xpinjection.com 
@xpinjection

2

Java concurrency in practice

3

Disclaimer 
This is all my 
personal experience

4

You could follow my slides 
http://slidesha.re/1vCS9dx

5

Lets start from practical tasks!

6

Java concurrency in practice

7

#1. Graceful shutdown 
SERVICE 1 SERVICE 2 
DB 
async jobs 
long batch 
operations

8

Shared boolean flag

9

#2. Retry on error task

10

Configurable retry until done

11

#3. Concurrent task executor 
TASK EXECUTOR 
SERVICE 
add task 
wait for tasks completion

12

Submit tasks in single thread

13

Limit concurrent tasks

14

Wait for single or all tasks

15

Get results and reduce count

16

#4. Resource locking 
RESOURCE A 
THREAD 1 
THREAD 2 
WAIT 
THREAD 3 RESOURCE B

17

Simple map to store locks

18

Count lock usages to remove it

19

Reduce synchronization penalty

20

#5. Inter-process named locks 
RESOURCE 
THREAD 1 
THREAD 2 
THREAD 3 
WAIT WAIT 
PROCESS 1 PROCESS 2

21

Locks are stored in simple map

22

On error throw own exception

23

#6. Limit resource access rate 
RESOURCE 
THREAD 1 
THREAD 2 
WAIT 
THREAD 1 
THREAD 2 
3 REQ/SEC

24

Use Semaphore as limiter

25

Work with current semaphore

26

Reset not used semaphores

27

#7. Transparent batching 
TASK EXECUTOR 
THREAD 1 
add task 
wait for results 
THREAD 2 
THREAD 3 
wait for results 
add task 
add task 
wait for results

28

Just add task to task executor

29

Tasks processed in batches

30

Wait for first item opening batch

31

Take full batch for processing

32

#8. Resource access control 
RESOURCE A 
THREAD 1 
THREAD 2 
ALLOW 
FORBID 
ALLOW 
THREAD 3 RESOURCE B

33

Check if resource access allowed

34

Access success is based on time

35

Check for successful access

36

#9. Sharded quick counters

37

LongAdder algorithm

38

Lets review useful tools!

39

#1. Thread factory builder

40

#2. ConcurrentUtils

41

#3. TimedSemaphore

42

#4. TimeLimiter

43

#5. RateLimiter

44

#6. Striped

45

#7. ListenableFuture

46

#8. Uninterruptibles

47

#9. CLHQueueLock

48

Spin lock implementation

49

#10. Disruptor

50

#11. ZooKeeper + Curator 
• Elections 
• Locks 
• Barriers 
• Counters 
• Caches 
• Nodes 
• Queues

51

Useful links 
• https://code.google.com/p/guava-libraries/ 
• http://commons.apache.org/proper/commons-lang/ 
• http://dou.ua/lenta/articles/clh-lock/ 
• http://habrahabr.ru/company/luxoft/blog/157273/ 
• http://habrahabr.ru/post/130113/ 
• http://lmax-exchange.github.io/disruptor/ 
• http://zookeeper.apache.org/ 
• http://curator.apache.org/ 
• http://en.wikipedia.org/wiki/Ctrie 
• https://code.google.com/p/guava-libraries/wiki/ListenableFutureExplained 
• https://guava-libraries.googlecode.com/files/guava-concurrent-slides.pdf

52

And remember…

53

@xpinjection 
http://xpinjection.com 
mikalai.alimenkou@xpinjection.com

More Related Content

Java concurrency in practice