Output when the code runs:
Missy gave birth to 2 kittens
Coco gave birth to 2 kittens
Kitten 1 (mother - Missy) got out of the basket
Kitten 1 (mother - Coco) got out of the basket
Kitten 2 (mother - Missy) got out of the basket
Kitten 2 (mother - Coco) got out of the basket
Missy: All the kittens are in the basket. Missy brought them back
Coco: All the kittens are in the basket. Coco brought them back
package com.codegym.task.task16.task1610;
/*
Arranging calls to join()
*/
public class Solution {
public static void main(String[] args) throws InterruptedException {
Cat cat1 = new Cat("Missy");
Cat cat2 = new Cat("Coco");
cat2.join();
}
private static void investigateWorld() {
}
public static class Cat extends Thread {
protected Kitten kitten1;
protected Kitten kitten2;
public Cat(String name) {
super(name);
kitten1 = new Kitten("Kitten 1 (mother - " + getName() + ")");
kitten2 = new Kitten("Kitten 2 (mother - " + getName() + ")");
start();
}
public void run() {
System.out.println(getName() + " gave birth to 2 kittens");
try {
initAllKittens();
} catch (InterruptedException e) {
}
System.out.println(getName() + ": All the kittens are in the basket. " + getName() + " brought them back");
}
private void initAllKittens() throws InterruptedException {
kitten1.start();
kitten1.sleep(200);
kitten2.start();
kitten2.sleep(200);
}
}
public static class Kitten extends Thread {
public Kitten(String name) {
super(name);
}
public void run() {
System.out.println(getName() + " got out of the basket");
investigateWorld();
}
}
}