java ( 325737 ) - com.iluwatar.re ( 326196 ) stack: com.iluwatar.reader.writer.lock.App.main(App.java:56) org.codehaus.mojo.exec.ExecJavaMojo.doMain(ExecJavaMojo.java:385) org.codehaus.mojo.exec.ExecJavaMojo.doExec(ExecJavaMojo.java:374) org.codehaus.mojo.exec.ExecJavaMojo.lambda$execute$0(ExecJavaMojo.java:296) java.base/java.lang.Thread.run(Thread.java:840)
/*
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
*
* The MIT License
* Copyright © 2014-2022 Ilkka Seppälä
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.iluwatar.reader.writer.lock;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
/**
* In a multiple thread applications, the threads may try to synchronize the shared resources
* regardless of read or write operation. It leads to a low performance especially in a "read more
* write less" system as indeed the read operations are thread-safe to another read operation.
*
* Reader writer lock is a synchronization primitive that try to resolve this problem. This
* pattern allows concurrent access for read-only operations, while write operations require
* exclusive access. This means that multiple threads can read the data in parallel but an exclusive
* lock is needed for writing or modifying data. When a writer is writing the data, all other
* writers or readers will be blocked until the writer is finished writing.
*
*
This example use two mutex to demonstrate the concurrent access of multiple readers and
* writers.
*
* @author hongshuwei@gmail.com
*/
@Slf4j
public class App {
/**
* Program entry point.
*
* @param args command line args
*/
public static void main(String[] args) {
var executeService = Executors.newFixedThreadPool(10);
var lock = new ReaderWriterLock();
// Start writers
for (var i = 0; i < 5; i++) {
var writingTime = ThreadLocalRandom.current().nextLong(5000);
executeService.submit(new Writer("Writer " + i, lock.writeLock(), writingTime));
}
LOGGER.info("Writers added...");
// Start readers
for (var i = 0; i < 5; i++) {
var readingTime = ThreadLocalRandom.current().nextLong(10);
executeService.submit(new Reader("Reader " + i, lock.readLock(), readingTime));
}
LOGGER.info("Readers added...");
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
LOGGER.error("Error sleeping before adding more readers", e);
Thread.currentThread().interrupt();
}
// Start readers
for (var i = 6; i < 10; i++) {
var readingTime = ThreadLocalRandom.current().nextLong(10);
executeService.submit(new Reader("Reader " + i, lock.readLock(), readingTime));
}
LOGGER.info("More readers added...");
// In the system console, it can see that the read operations are executed concurrently while
// write operations are exclusive.
executeService.shutdown();
try {
executeService.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
LOGGER.error("Error waiting for ExecutorService shutdown", e);
Thread.currentThread().interrupt();
}
}
}
| No. | From | Name | Value |
|---|---|---|---|
| 1 | class@56 | LOGGER | Logger[com.iluwatar.reader.writer.lock.App] |
| 2 | 56 | args | [Ljava.lang.String;@6305147a |
| END | 0 | 0 | 0 |
| Process Filter | Thread Filter | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 325737 java | 326239 pool-1-thread-1 | 326243 pool-1-thread-5 | 326196 com.iluwatar.re | 326252 pool-1-thread-9 | 326253 pool-1-thread-1 | 326242 pool-1-thread-4 | 326250 pool-1-thread-7 | 326251 pool-1-thread-8 | 326240 pool-1-thread-2 | 326241 pool-1-thread-3 | 326245 pool-1-thread-6 |
| No. | PN | PID | TID | TN | T | L | Message |
|---|---|---|---|---|---|---|---|
| 1 | java | 325737 | 326239 | pool-1-thread-1 | com.iluwatar.reader.writer.lock.Writer | I | Writer 0 begin |
| 2 | java | 325737 | 326196 | com.iluwatar.re | com.iluwatar.reader.writer.lock.App | I | Writers added... |
| 3 | java | 325737 | 326196 | com.iluwatar.re | com.iluwatar.reader.writer.lock.App | I | Readers added... |
| 4 | java | 325737 | 326239 | pool-1-thread-1 | com.iluwatar.reader.writer.lock.Writer | I | Writer 0 finished after writing 3512ms |
| 5 | java | 325737 | 326240 | pool-1-thread-2 | com.iluwatar.reader.writer.lock.Writer | I | Writer 1 begin |
| 6 | java | 325737 | 326240 | pool-1-thread-2 | com.iluwatar.reader.writer.lock.Writer | I | Writer 1 finished after writing 859ms |
| 7 | java | 325737 | 326253 | pool-1-thread-1 | com.iluwatar.reader.writer.lock.Reader | I | Reader 4 begin |
| 8 | java | 325737 | 326252 | pool-1-thread-9 | com.iluwatar.reader.writer.lock.Reader | I | Reader 3 begin |
| 9 | java | 325737 | 326245 | pool-1-thread-6 | com.iluwatar.reader.writer.lock.Reader | I | Reader 0 begin |
| 10 | java | 325737 | 326250 | pool-1-thread-7 | com.iluwatar.reader.writer.lock.Reader | I | Reader 1 begin |
| 11 | java | 325737 | 326251 | pool-1-thread-8 | com.iluwatar.reader.writer.lock.Reader | I | Reader 2 begin |
| 12 | java | 325737 | 326250 | pool-1-thread-7 | com.iluwatar.reader.writer.lock.Reader | I | Reader 1 finish after reading 0ms |
| 13 | java | 325737 | 326252 | pool-1-thread-9 | com.iluwatar.reader.writer.lock.Reader | I | Reader 3 finish after reading 2ms |
| 14 | java | 325737 | 326245 | pool-1-thread-6 | com.iluwatar.reader.writer.lock.Reader | I | Reader 0 finish after reading 3ms |
| 15 | java | 325737 | 326251 | pool-1-thread-8 | com.iluwatar.reader.writer.lock.Reader | I | Reader 2 finish after reading 2ms |
| 16 | java | 325737 | 326253 | pool-1-thread-1 | com.iluwatar.reader.writer.lock.Reader | I | Reader 4 finish after reading 9ms |
| 17 | java | 325737 | 326242 | pool-1-thread-4 | com.iluwatar.reader.writer.lock.Writer | I | Writer 3 begin |
| 18 | java | 325737 | 326196 | com.iluwatar.re | com.iluwatar.reader.writer.lock.App | I | More readers added... |
| 19 | java | 325737 | 326242 | pool-1-thread-4 | com.iluwatar.reader.writer.lock.Writer | I | Writer 3 finished after writing 4506ms |
| 20 | java | 325737 | 326243 | pool-1-thread-5 | com.iluwatar.reader.writer.lock.Writer | I | Writer 4 begin |
| 21 | java | 325737 | 326243 | pool-1-thread-5 | com.iluwatar.reader.writer.lock.Writer | I | Writer 4 finished after writing 4852ms |
| 22 | java | 325737 | 326239 | pool-1-thread-1 | com.iluwatar.reader.writer.lock.Reader | I | Reader 6 begin |
| 23 | java | 325737 | 326252 | pool-1-thread-9 | com.iluwatar.reader.writer.lock.Reader | I | Reader 9 begin |
| 24 | java | 325737 | 326250 | pool-1-thread-7 | com.iluwatar.reader.writer.lock.Reader | I | Reader 8 begin |
| 25 | java | 325737 | 326240 | pool-1-thread-2 | com.iluwatar.reader.writer.lock.Reader | I | Reader 7 begin |
| 26 | java | 325737 | 326240 | pool-1-thread-2 | com.iluwatar.reader.writer.lock.Reader | I | Reader 7 finish after reading 1ms |
| 27 | java | 325737 | 326239 | pool-1-thread-1 | com.iluwatar.reader.writer.lock.Reader | I | Reader 6 finish after reading 9ms |
| 28 | java | 325737 | 326252 | pool-1-thread-9 | com.iluwatar.reader.writer.lock.Reader | I | Reader 9 finish after reading 8ms |
| 29 | java | 325737 | 326250 | pool-1-thread-7 | com.iluwatar.reader.writer.lock.Reader | I | Reader 8 finish after reading 7ms |
| 30 | java | 325737 | 326241 | pool-1-thread-3 | com.iluwatar.reader.writer.lock.Writer | I | Writer 2 begin |
| 31 | java | 325737 | 326241 | pool-1-thread-3 | com.iluwatar.reader.writer.lock.Writer | I | Writer 2 finished after writing 2761ms |
| END | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
Functions and Shortcuts
| No. | Function | Shortcuts | Description |
|---|---|---|---|
| 1 | GB | Alt + LEFT, Alt + A | Go Backward |
| 2 | GF | Alt + RIGHT, Alt + D | Go Foreward |
| 3 | PPE | Alt + UP, Alt + W | Previous Process End |
| 4 | NPS | Alt + DOWN, Alt + S | Next Process Start |
| 5 | PB | Ctrl + LEFT, Ctrl + A | current Process Backward |
| 6 | PF | Ctrl + RIGHT, Ctrl + D | current Process Foreward |
| 7 | PPTE | Ctrl + UP, Ctrl + W | go to current Process's Previous Thread's End |
| 8 | PNTS | Ctrl + DOWN, Ctrl + S | go to current Process's Next Thread's Start |
| 9 | TB | LEFT, A | current Thread Backward |
| 10 | TF | RIGHT, D | current Thread Foreward |
| 11 | LU | UP, W | go Line Up of current code block in current thread |
| 12 | LD | DOWN, S | go Line Down of current code block in current thread |
| 13 | LP | Shift + UP, Shift + W | go to the occurrence of current line in Previous Loop |
| 14 | LD | Shift + DOWN, Shift + S | go to the occurrence of current line in Next Loop |
| 15 | BS | Home | go to code Block Start |
| 16 | BE | End | go to code Block End |
| Project: | JavaDesignPatterns |
| Update: | 20240509 |
| Commit: | bf6456ba6 |
| Source Code: | reader-writer-lock |
| BuildTool: | Java17 |
| Compiler: | Java17 |
| Runtime: | Openjdk17 |
| System: | MySystemD |
| Kernel: | Linux5.10.211 |
| Cpu: | Intel:Corei7-7700K |
| Machine: | AwesomeMachine |