1 package org.prevayler.foundation; 2 3 4 6 public class Turn { 7 8 private Turn _next; 9 private int _tickets = 0; 10 private boolean _isAlwaysSkipped; 11 12 13 public static Turn first() { return new Turn(1000000); } 15 private Turn(int tickets) { _tickets = tickets; } 16 17 public Turn next() { 18 if (_next == null) _next = new Turn(0); 19 return _next; 20 } 21 22 public synchronized void start() { 23 if (_tickets == 0) Cool.wait(this); 24 _tickets--; 25 } 26 27 public void end() { 28 next().haveSomeTickets(1); 29 } 30 31 private synchronized void haveSomeTickets(int tickets) { 32 if (_isAlwaysSkipped) { 33 next().haveSomeTickets(tickets); 34 return; 35 } 36 _tickets += tickets; 37 notify(); 38 } 39 40 public synchronized void alwaysSkip() { 41 end(); 42 _isAlwaysSkipped = true; 43 next().haveSomeTickets(_tickets); 44 } 45 46 } 47 | Popular Tags |