|
| 1 | +package main.concurrency; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | +import java.util.concurrent.ThreadLocalRandom; |
| 7 | +import java.util.concurrent.locks.Lock; |
| 8 | +import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 9 | + |
| 10 | +/** |
| 11 | + * Sample of using ReentrantReadWriteLock |
| 12 | + * A box with some items and 5 worker threads that can randomly add, remove, peek or print the items |
| 13 | + * Each worker has 5 of those above operations that is chosen also randomly |
| 14 | + */ |
| 15 | +public class ReentrantReadWriteLockDemo { |
| 16 | + |
| 17 | + private static final Integer[] array = {1,2,3,4,5}; |
| 18 | + private static final Box box = new Box(array); |
| 19 | + |
| 20 | + private static class Box { |
| 21 | + private final List<Integer> items = new ArrayList<>(); |
| 22 | + private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock(); |
| 23 | + private final Lock r = rwl.readLock(); |
| 24 | + private final Lock w = rwl.writeLock(); |
| 25 | + |
| 26 | + private Box(Integer[] array) { |
| 27 | + Collections.addAll(items, array); |
| 28 | + } |
| 29 | + |
| 30 | + int peek() { |
| 31 | + r.lock(); |
| 32 | + try { return items.get(0); } |
| 33 | + finally { r.unlock(); } |
| 34 | + } |
| 35 | + |
| 36 | + String print() { |
| 37 | + r.lock(); |
| 38 | + try { |
| 39 | + StringBuilder sb = new StringBuilder("|"); |
| 40 | + items.forEach(c -> sb.append(c).append("|")); |
| 41 | + return sb.toString(); |
| 42 | + } |
| 43 | + finally { r.unlock(); } |
| 44 | + } |
| 45 | + |
| 46 | + int remove() { |
| 47 | + w.lock(); |
| 48 | + try { |
| 49 | + if (!items.isEmpty()) { |
| 50 | + return items.remove(0); |
| 51 | + } |
| 52 | + return 0; |
| 53 | + } |
| 54 | + finally { w.unlock(); } |
| 55 | + } |
| 56 | + |
| 57 | + void add(int i) { |
| 58 | + w.lock(); |
| 59 | + try { items.add(i); } |
| 60 | + finally { w.unlock(); } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private static class Worker implements Runnable { |
| 65 | + private static final int MAX_OP = 5; |
| 66 | + private String name; |
| 67 | + private boolean stop = false; |
| 68 | + private int count = 1; |
| 69 | + |
| 70 | + private Worker(String name) { |
| 71 | + this.name = name; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void run() { |
| 76 | + while (!stop) { |
| 77 | + String info = String.format("%s [%d/%d]", this.name, count, MAX_OP); |
| 78 | + int r = ThreadLocalRandom.current().nextInt(0,5); |
| 79 | + switch (r) { |
| 80 | + case 1: |
| 81 | + int n = ThreadLocalRandom.current().nextInt(6,10); |
| 82 | + box.add(n); |
| 83 | + System.out.println(info + " added item " + n); |
| 84 | + break; |
| 85 | + case 2: |
| 86 | + int re = box.remove(); |
| 87 | + System.out.println(info + " removed item " + re); |
| 88 | + break; |
| 89 | + case 3: |
| 90 | + int pe = box.peek(); |
| 91 | + System.out.println(info + " peeked item " + pe); |
| 92 | + break; |
| 93 | + default: |
| 94 | + String s = box.print(); |
| 95 | + System.out.println(info + " printed list " + s); |
| 96 | + break; |
| 97 | + } |
| 98 | + try { |
| 99 | + Thread.sleep(ThreadLocalRandom.current().nextInt(500,2000)); |
| 100 | + if (count == MAX_OP) { |
| 101 | + this.stop(); |
| 102 | + } |
| 103 | + count++; |
| 104 | + } catch (InterruptedException e) { |
| 105 | + e.printStackTrace(); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private void stop() { |
| 111 | + this.stop = true; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public static void main(String[] args) { |
| 116 | + Thread t; |
| 117 | + for (int i = 1; i <= 5; i++) { |
| 118 | + t = new Thread(new Worker("Worker" + i)); |
| 119 | + t.start(); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments