fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.Random;
  7.  
  8. /**
  9.  * Example 92 - Mutual Exclusion
  10.  */
  11.  
  12. class Util {
  13. public static void pause(int low, int high) {
  14. Random r = new Random();
  15. int R = r.nextInt(high-low) +low;
  16. try {
  17. Thread.sleep(R);
  18. } catch (InterruptedException e) {
  19. e.printStackTrace();
  20. }
  21. }
  22.  
  23. public static int random(int low, int high) {
  24. Random r = new Random();
  25. int R = r.nextInt(high-low) + low;
  26. return R;
  27. }
  28.  
  29. }
  30. class Printer extends Thread {
  31. static Object mutex = new Object();
  32. public void run() {
  33. for(;;) {
  34. synchronized (mutex) {
  35. System.out.print("-");
  36. Util.pause(100, 300);
  37. System.out.print("/");
  38. }
  39. Util.pause(0, 200);
  40. }
  41. }
  42. }
  43. class MutualExclusion {
  44. public static void main(String[] args) {
  45. new Printer().start();
  46. new Printer().start();
  47. }
  48. }
  49.  
Time limit exceeded #stdin #stdout 5s 28188KB
stdin
Standard input is empty
stdout
-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-/-