1 4 package com.tc.util; 5 6 import com.tc.exception.TCRuntimeException; 7 8 public class Counter { 9 private int count; 10 11 public Counter() { 12 this(0); 13 } 14 15 public Counter(int initial) { 16 this.count = initial; 17 } 18 19 public int increment() { 20 return increment(1); 21 } 22 23 public synchronized int increment(int val) { 24 count += val; 25 notifyAll(); 26 return count; 27 } 28 29 public int decrement() { 30 return decrement(1); 31 } 32 33 public synchronized int decrement(int val) { 34 count -= val; 35 notifyAll(); 36 return count; 37 } 38 39 public synchronized int get() { 40 return count; 41 } 42 43 public synchronized void waitUntil(int value) { 44 while (this.count != value) { 45 try { 46 wait(); 47 } catch (InterruptedException e) { 48 throw new TCRuntimeException(e); 49 } 50 } 51 } 52 } 53 | Popular Tags |