1 11 package org.eclipse.jdt.internal.core.search.indexing; 12 13 17 public class ReadWriteMonitor { 18 19 24 private int status = 0; 25 29 public synchronized void enterRead() { 30 while (status < 0) { 31 try { 32 wait(); 33 } catch(InterruptedException e) { 34 } 36 } 37 status++; 38 } 39 43 public synchronized void enterWrite() { 44 while (status != 0) { 45 try { 46 wait(); 47 } catch(InterruptedException e) { 48 } 50 } 51 status--; 52 } 53 56 public synchronized void exitRead() { 57 58 if (--status == 0) notifyAll(); 59 } 60 64 public synchronized void exitWrite() { 65 66 if (++status == 0) notifyAll(); 67 } 68 74 public synchronized boolean exitReadEnterWrite() { 75 if (status != 1) return false; 77 status = -1; 78 return true; 79 } 80 93 public synchronized void exitWriteEnterRead() { 94 this.exitWrite(); 95 this.enterRead(); 96 } 97 public String toString() { 98 StringBuffer buffer = new StringBuffer (); 99 if (status == 0) { 100 buffer.append("Monitor idle "); } else if (status < 0) { 102 buffer.append("Monitor writing "); } else if (status > 0) { 104 buffer.append("Monitor reading "); } 106 buffer.append("(status = "); buffer.append(this.status); 108 buffer.append(")"); return buffer.toString(); 110 } 111 } 112 | Popular Tags |