Mutex.java 457 B

123456789101112131415161718192021222324252627
  1. public abstract class Mutex {
  2. public static void criticalSection() {
  3. try {
  4. Thread.sleep((int) (Math.random() * 3000));
  5. }
  6. catch (InterruptedException e) {
  7. // Nothing
  8. }
  9. }
  10. public static void nonCriticalSection() {
  11. try {
  12. Thread.sleep((int) (Math.random() * 3000));
  13. }
  14. catch (InterruptedException e) {
  15. // Nothing
  16. }
  17. }
  18. // Take ownership
  19. public abstract void p(int id);
  20. // Release ownership
  21. public abstract void v(int id);
  22. }