1 11 package org.eclipse.core.internal.registry; 12 13 19 public class ReadWriteMonitor { 20 21 26 private int status = 0; 27 28 private Thread writeLockowner; 29 30 34 public synchronized void enterRead() { 35 if (writeLockowner == Thread.currentThread()) 36 return; 37 while (status < 0) { 38 try { 39 wait(); 40 } catch (InterruptedException e) { 41 } 43 } 44 status++; 45 } 46 47 51 public synchronized void enterWrite() { 52 if (writeLockowner != Thread.currentThread()) { 53 while (status != 0) { 54 try { 55 wait(); 56 } catch (InterruptedException e) { 57 } 59 } 60 writeLockowner = Thread.currentThread(); 62 } 63 status--; 64 } 65 66 69 public synchronized void exitRead() { 70 if (writeLockowner == Thread.currentThread()) 71 return; 72 if (--status == 0) 73 notifyAll(); 74 } 75 76 80 public synchronized void exitWrite() { 81 if (writeLockowner != Thread.currentThread()) 82 throw new IllegalStateException ("Current owner is " + writeLockowner); if (++status == 0) { 84 writeLockowner = null; 86 notifyAll(); 87 } 88 } 89 90 public String toString() { 91 StringBuffer buffer = new StringBuffer (); 92 buffer.append(this.hashCode()); 93 if (status == 0) { 94 buffer.append("Monitor idle "); } else if (status < 0) { 96 buffer.append("Monitor writing "); } else if (status > 0) { 98 buffer.append("Monitor reading "); } 100 buffer.append("(status = "); buffer.append(this.status); 102 buffer.append(")"); return buffer.toString(); 104 } 105 } 106 | Popular Tags |