package com.codegym.task.task16.task1622;

/*
Consecutive threads

*/

public class Solution {
    public volatile static int COUNT = 4;

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < COUNT; i++) {
            SleepingThread s1 = new SleepingThread();
            s1.join();
            SleepingThread s2 = new SleepingThread();
            s2.join();
            SleepingThread s3 = new SleepingThread();
            s3.join();
            SleepingThread s4 = new SleepingThread();
            s4.join();
            //write your code here
        }
    }

    public static class SleepingThread extends Thread {
        private static volatile int threadCount = 0;
        private volatile int countdownIndex = COUNT;

        public SleepingThread() {
            super(String.valueOf(++threadCount));
            start();
        }

        public void run() {
            while (true) {
                System.out.println(this);
                if (--countdownIndex == 0) return;
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    System.out.println("Thread interrupted");
                }
                //write your code here
            }
        }

        public String toString() {
            return "#" + getName() + ": " + countdownIndex;
        }
    }
}