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.concurrent.locks.ReentrantLock;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static class Person extends Thread { //Человек
  12.  
  13. public Person(String name) {
  14.  
  15. super(name);
  16. run();
  17. }
  18.  
  19. @Override
  20. public void run() {
  21. Iron.ironLock.lock();
  22. try {
  23. Iron iron = takeIron();
  24. Clothes clothes = takeClothes();
  25. ironing(iron, clothes);
  26. returnIron();
  27. } finally {
  28. Iron.ironLock.unlock();
  29. }
  30. }
  31.  
  32. protected Iron takeIron() {
  33. System.out.println("Taking an Iron");
  34. return new Iron();
  35. }
  36.  
  37. protected Iron returnIron() {
  38. System.out.println("Returning the Iron");
  39. return new Iron();
  40. }
  41.  
  42. protected Clothes takeClothes() {
  43. return new Clothes("T-shirt");
  44. }
  45.  
  46. protected void ironing(Iron iron, Clothes clothes) {
  47. System.out.println(getName() + "'s ironing the " + clothes.name);
  48. }
  49. }
  50.  
  51. public static class Iron {
  52. static ReentrantLock ironLock = new ReentrantLock();
  53. } //Утюг
  54.  
  55. public static class Clothes {//Одежда
  56. String name;
  57.  
  58. public Clothes(String name) {
  59. this.name = name;
  60. }
  61. }
  62.  
  63. public static void main (String[] args) throws java.lang.Exception
  64. {
  65. // your code goes here
  66. Person diana = new Person("Diana");
  67. Person igor = new Person("Igor");
  68. }
  69. }
  70.  
  71.  
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
Taking an Iron
Diana's ironing the T-shirt
Returning the Iron
Taking an Iron
Igor's ironing the T-shirt
Returning the Iron