Task.java 571 B

12345678910111213141516171819202122232425262728293031
  1. class Task extends Thread {
  2. private String name;
  3. private int id;
  4. private Mutex critical;
  5. public Task(String name_, int id_, Mutex critical_) {
  6. name = name_;
  7. id = id_;
  8. critical = critical_;
  9. }
  10. public void describe() {
  11. System.out.println(id + "<" + critical + ">:" + name);
  12. }
  13. public void run() {
  14. while (true) {
  15. critical.p(id);
  16. System.out.println( name + " is CRITICAL!");
  17. Mutex.criticalSection(((Integer)id).toString());
  18. System.out.println( name + " isn't critical anymore.");
  19. critical.v(id);
  20. Mutex.nonCriticalSection();
  21. }
  22. }
  23. }