1) What is the point of class-level lock of the Iron class? This class does not contain any methods. Then what does it lock in fact??
2) Why is the (Person.class) lock not correct solution? The output appeared in the correct order (as if there were only 1 piece of iron).
3) When I wrote this: synchronized(Clothes.class), the output appeared in the correct order.
When I wrote this: synchronized (Solution.class), the output still appeared in the correct order.
So that means perhaps is it irrelevant what I write into the parentheses?? ...
But the same question just in other words: what is the meaning of "synchronized" with lock of one class, and what is the meaning of "synchronized" with lock of another class? What is the difference?
package com.codegym.task.task17.task1718;
/*
Ironing
*/
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 (Person.class) {
Iron iron = takeIron();
Clothes clothes = takeClothes();
iron(iron, clothes);
returnIron();
}
}
protected Iron takeIron() {
System.out.println("Taking the iron");
return new Iron();
}
protected Iron returnIron() {
System.out.println("Returning the iron");
return new Iron();
}
protected Clothes takeClothes() {
return new Clothes("T-shirt");
}
protected void iron(Iron iron, Clothes clothes) {
System.out.println(getName() + " is ironing a " + clothes.name);
}
}
public static class Iron {
}
public static class Clothes {
String name;
public Clothes(String name) {
this.name = name;
}
}
}