Advertisement
jaVer404

level17.lesson10.home06

Oct 2nd, 2015
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.64 KB | None | 0 0
  1. package com.javarush.test.level17.lesson10.home06;
  2.  
  3. /* Глажка
  4. И снова быт...
  5. Поставьте один synchronized, чтобы diana и igor гладили по-очереди, ведь утюг всего один!
  6.  
  7. Подсказка: использовать блокировку на уровне класса.
  8. */
  9.  
  10. public class Solution {
  11.     public static void main(String[] args) {
  12.         Person diana = new Person("Diana");
  13.         Person igor = new Person("Igor");
  14.     }
  15.  
  16.     public static class Person extends Thread { //Человек
  17.         public Person(String name) {
  18.             super(name);
  19.             start();
  20.         }
  21.  
  22.         @Override
  23.         public void run() {
  24.             Iron iron = takeIron();
  25.             Clothes clothes = takeClothes();
  26.             ironing(iron, clothes);
  27.             returnIron();
  28.         }
  29.         protected Iron takeIron() {
  30.             synchronized (Iron.class) {
  31.             System.out.println("Taking an Iron");
  32.             return new Iron();}
  33.         }
  34.  
  35.         protected Iron returnIron() {
  36.             System.out.println("Returning the Iron");
  37.             return new Iron();
  38.         }
  39.  
  40.         protected Clothes takeClothes() {
  41.             return new Clothes("T-shirt");
  42.         }
  43.  
  44.         protected void ironing(Iron iron, Clothes clothes) {
  45.             System.out.println(getName() + "'s ironing the " + clothes.name);
  46.         }
  47.     }
  48.  
  49.     public static class Iron {
  50.  
  51.     } //Утюг
  52.  
  53.     public static class Clothes {//Одежда
  54.         String name;
  55.  
  56.         public Clothes(String name) {
  57.             this.name = name;
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement