|
| 1 | +import java.util.concurrent.ExecutorService; |
| 2 | +import java.util.concurrent.Executors; |
| 3 | +import java.util.concurrent.locks.ReadWriteLock; |
| 4 | +import java.util.concurrent.locks.ReentrantReadWriteLock; |
| 5 | + |
| 6 | +public class SharedStateMultiReader { |
| 7 | + // shared resource |
| 8 | + private volatile int data; |
| 9 | + // lock for the resource |
| 10 | + ReadWriteLock rwLock = new ReentrantReadWriteLock(); |
| 11 | + |
| 12 | + private static final int READ_CYCLES = (int)1e6; |
| 13 | + private static final int WRITE_CYCLES = (int)1e2; |
| 14 | + private static final int READER_THREADS = 100; // read heavy systems |
| 15 | + private static final int WRITER_THREADS = 10; |
| 16 | + |
| 17 | + public SharedStateMultiReader(int initialData) { |
| 18 | + this.data = initialData; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Retrieves the data from a private static variable. |
| 23 | + * Representing a shared resource |
| 24 | + * |
| 25 | + * @return The data value stored |
| 26 | + */ |
| 27 | + private int getData() { |
| 28 | + rwLock.readLock().lock(); |
| 29 | + int value = 0; |
| 30 | + try { |
| 31 | + value = data; |
| 32 | + } finally { |
| 33 | + rwLock.readLock().unlock(); |
| 34 | + } |
| 35 | + return value; |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Updates the value of the private static variable 'data'. |
| 40 | + */ |
| 41 | + private void updateData() { |
| 42 | + rwLock.writeLock().lock(); |
| 43 | + try { |
| 44 | + data += 1; |
| 45 | + } finally { |
| 46 | + rwLock.writeLock().unlock(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public static void main(String ...args) throws InterruptedException { |
| 51 | + final long startTime = System.nanoTime(); |
| 52 | + SharedStateMultiReader sharedState = new SharedStateMultiReader(0); |
| 53 | + Thread []readers = new Thread[READER_THREADS]; |
| 54 | + for(int readerId = 0; readerId < READER_THREADS; readerId++){ |
| 55 | + final int threadId = readerId; |
| 56 | + readers[threadId] = new Thread(new Runnable() { |
| 57 | + @Override |
| 58 | + public void run() { |
| 59 | + for(int cycles = 0; cycles < READ_CYCLES; cycles++) { |
| 60 | + int value = sharedState.getData(); |
| 61 | + // to keep I/O low to influence perf |
| 62 | + if(cycles % (READ_CYCLES/10) == 0){ |
| 63 | + // System.out.format("read threadId: %d read_value: %d\n", threadId, value); |
| 64 | + // System.out.flush(); |
| 65 | + } |
| 66 | + } |
| 67 | + System.out.format("\nread threadId: %d finished", threadId); |
| 68 | + System.out.flush(); |
| 69 | + } |
| 70 | + }); |
| 71 | + readers[threadId].start(); |
| 72 | + } |
| 73 | + Thread []writers = new Thread[WRITER_THREADS]; |
| 74 | + for(int writerId = 0; writerId < WRITER_THREADS; writerId++){ |
| 75 | + final int threadId = writerId; |
| 76 | + writers[threadId] = new Thread(new Runnable() { |
| 77 | + @Override |
| 78 | + public void run() { |
| 79 | + for(int cycles = 0; cycles < WRITE_CYCLES; cycles++) { |
| 80 | + sharedState.updateData(); |
| 81 | + if(cycles % (WRITE_CYCLES/10) == 0){ |
| 82 | + // int value = sharedState.getData(); |
| 83 | + // System.out.format("write threadId: %d write_value: %d\n", threadId, value); |
| 84 | + // System.out.flush(); |
| 85 | + } |
| 86 | + } |
| 87 | + System.out.format("\nwrite threadId: %d finished", threadId); |
| 88 | + System.out.flush(); |
| 89 | + } |
| 90 | + }); |
| 91 | + writers[threadId].start(); |
| 92 | + } |
| 93 | + |
| 94 | + for(int readerId = 0; readerId < READER_THREADS; readerId++){ |
| 95 | + readers[readerId].join(); |
| 96 | + } |
| 97 | + for(int writerId = 0; writerId < WRITER_THREADS; writerId++){ |
| 98 | + writers[writerId].join(); |
| 99 | + } |
| 100 | + final long duration = System.nanoTime() - startTime; |
| 101 | + System.out.println("SharedStateMultiReader time taken(sec): " + duration/(1e9)); |
| 102 | + } |
| 103 | +} |
0 commit comments