fork download
  1. import java.util.concurrent.locks.ReentrantLock;
  2.  
  3. public class Program {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. CommonResource commonResource= new CommonResource();
  8. ReentrantLock locker = new ReentrantLock(); // создаем заглушку
  9. for (int i = 1; i < 6; i++){
  10.  
  11. Thread t = new Thread(new CountThread(commonResource, locker));
  12. t.setName("Thread "+ i);
  13. t.start();
  14. }
  15. }
  16. }
  17.  
  18. class CommonResource{
  19.  
  20. int x=0;
  21. }
  22.  
  23. class CountThread implements Runnable{
  24.  
  25. CommonResource res;
  26. ReentrantLock locker;
  27. CountThread(CommonResource res, ReentrantLock lock){
  28. this.res=res;
  29. locker = lock;
  30. }
  31. public void run(){
  32.  
  33. locker.lock(); // устанавливаем блокировку
  34. try{
  35. res.x=1;
  36. for (int i = 1; i < 5; i++){
  37. System.out.printf("%s %d \n", Thread.currentThread().getName(), res.x);
  38. res.x++;
  39. Thread.sleep(100);
  40. }
  41. }
  42. System.out.println(e.getMessage());
  43. }
  44. finally{
  45. locker.unlock(); // снимаем блокировку
  46. }
  47. }
  48. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:3: error: class Program is public, should be declared in a file named Program.java
public class Program {
       ^
1 error
stdout
Standard output is empty