Read-Write Lock
서로 다른 조작 간에 대상 임계구에 서로 다른 차이를 초래할 때 Read-Write 잠금 모드를 고려할 수 있다. 즉, 조작 중 일부는 바뀌고 일부는 바뀌지 않는다(약간 돌아가지만 이해한 것만 있으면 알 수 있다).
// , Data
public class ReaderThread extends Thread {
private final Data data;
public ReaderThread(Data data) {
this.data = data;
}
public void run() {
try {
while (true) {
char[] readbuf;
readbuf = data.read();
System.out.println(getName() + String.valueOf(readbuf));
}
} catch (InterruptedException e) {
}
}
}
//
public class WriterThread extends Thread{
private final Data data;
private final String filler;
private int index=0;
public WriterThread(Data data,String filler){
this.data=data;
this.filler=filler;
}
public void run(){
try{
while(true){
char c=nextchar();
data.write(c);
sleep(1500);
}
}catch(InterruptedException e){
}
}
private char nextchar(){
char c=filler.charAt(index);
index++;
if(index>=filler.length()){
index=0;
}
return c;
}
}
public class Data {
private final char[] buffer;
private final ReadWriteLock lock = new ReadWriteLock();
public Data(int size) {
this.buffer = new char[size];
for (int i = 0; i < size; i++) {
buffer[i] = '*';
}
}
public char[] read() throws InterruptedException {
lock.readLock();
try {
return doRead();
} finally {
lock.readUnlock();
}
}
private char[] doRead() {
char[] newbuf = new char[buffer.length];
for (int i = 0; i < buffer.length; i++) {
newbuf[i] = buffer[i];
}
slowly();
return newbuf;
}
public void write(char c) throws InterruptedException {
lock.writeLock();
try {
doWrite(c);
} finally {
lock.writeUnlock();
}
}
public void doWrite(char c){
for(int i=0;i<buffer.length;i++){
buffer[i]=c;
slowly();
}
}
private void slowly() {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
}
}
}
//
public class ReadWriteLock {
private int readingReaders = 0;
private int waitingWriters = 0;
private int writingWriters = 0;
private boolean preferWriter = true;
public synchronized void readLock() throws InterruptedException {
while (writingWriters > 0 || (preferWriter && waitingWriters > 0)) {
wait();
}
readingReaders++;
}
public synchronized void readUnlock() {
readingReaders--;
preferWriter = true;
notifyAll();
}
public synchronized void writeLock() throws InterruptedException {
waitingWriters++;
try {
while (readingReaders > 0 || writingWriters > 0) {
wait();
}
} finally {
waitingWriters--;
}
writingWriters++;
}
public synchronized void writeUnlock(){
writingWriters--;
preferWriter=false;
notifyAll();
}
}
쓰기가 우선이기 때문에 읽기와 보기가 때때로 기다린다.
가장 중요한:
private int readingReaders = 0;//
private int waitingWriters = 0;//
private int writingWriters = 0;//
private boolean preferWriter = true;//
해결하기 위한 두 가지 충돌 문제: 1.읽기 및 쓰기 충돌 2.쓰기 충돌
읽기 자물쇠를 가져오는 경우, 쓰기 자물쇠를 가져오는 경우.똑똑히 정리하기만 하면 어떻게 쓰는지 안다.
이 모델은 쓰기 작업도 있고 읽기 작업도 있으며 읽기 작업이 쓰기 작업보다 빈번하고 심지어 읽기 작업도 시간이 많이 걸리는 상황에서 시간이 걸리지 않기 때문에 single thread execution보다 못하다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.