Achte darauf, dass der synchronized-Block das Iron-Objekt sperrt. <<< das ist die nachricht
aber die Solution klasse sollte diese doch abdecken?
package de.codegym.task.task17.task1718;
/*
Bügeln
*/
public class Solution {
public static void main(String[] args) {
Person diana = new Person("Diana");
Person steve = new Person("Steve");
diana.start();
steve.start();
}
public static class Person extends Thread {
public Person(String name) {
super(name);
}
@Override
public void run() {
synchronized(Solution.class){
Iron iron = takeIron();
Clothes clothes = takeClothes();
iron(iron, clothes);
returnIron();
}
}
protected Iron takeIron() {
System.out.println("Bügeleisen nehmen");
return new Iron();
}
protected Iron returnIron() {
System.out.println("Bügeleisen zurückgeben");
return new Iron();
}
protected Clothes takeClothes() {
return new Clothes("T-Shirt");
}
protected void iron(Iron iron, Clothes clothes) {
System.out.println(getName() + " bügelt ein " + clothes.name);
}
}
public static class Iron {
}
public static class Clothes {
String name;
public Clothes(String name) {
this.name = name;
}
}
}