Read-Write Lock

3282 단어
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보다 못하다.
       

좋은 웹페이지 즐겨찾기