public class TReadersWriters1 {
	private ReadersWriters1 rwLock;
	private class Thread1 implements Runnable {
		public void run() {
			for (int i=0; i<10; i++) {
				try {
					rwLock.startRead();
					System.out.println("Thread1 startRead");
					Thread.currentThread().sleep(100);
					System.out.println("Thread1 stopRead");
					rwLock.stopRead();
					Thread.currentThread().sleep(300);
				}
				catch (InterruptedException e) {
				}
			}
		}
	}
	private class Thread2 implements Runnable {
		public void run() {
			for (int i=0; i<10; i++) {
				try {
					rwLock.startRead();
					System.out.println("Thread2 startRead");
					Thread.currentThread().sleep(100);
					System.out.println("Thread2 stopRead");
					rwLock.stopRead();
					Thread.currentThread().sleep(900);
				}
				catch (InterruptedException e) {
				}
			}
		}
	}
	private class Thread3 implements Runnable {
		public void run() {
			for (int i=0; i<10; i++) {
				try {
					rwLock.startWrite();
					System.out.println("Thread3 startWrite");
					Thread.currentThread().sleep(100);
					System.out.println("Thread3 stopWrite");
					rwLock.stopWrite();
					Thread.currentThread().sleep(600);
				}
				catch (InterruptedException e) {
				}
			}
		}
	}
	private class Thread4 implements Runnable {
		public void run() {
			for (int i=0; i<10; i++) {
				try {
					rwLock.startWrite();
					System.out.println("Thread4 startWrite");
					Thread.currentThread().sleep(100);
					System.out.println("Thread4 stopWrite");
					rwLock.stopWrite();
					Thread.currentThread().sleep(1200);
				}
				catch (InterruptedException e) {
				}
			}
		}
	}
	private void doTest() {
		rwLock=new ReadersWriters1();
		Thread t1=new Thread(new Thread1());
		Thread t2=new Thread(new Thread2());
		Thread t3=new Thread(new Thread3());
		Thread t4=new Thread(new Thread4());
		t1.start();
		t2.start();
		t3.start();
		t4.start();
	}
	public static void main(String args[]) {
		TReadersWriters1 t=new TReadersWriters1();
		t.doTest();
	}
}

