package ru.szhernovoy.treads2;

import ru.szhernovoy.treads1.CounterValuesText;
import ru.szhernovoy.treads1.TokenCounter;

/**
 * Created by admin on 09.10.2016.
 */
public class CounterWithWait {
    private final long startTime;
    private final TokenCounter word;
    private final TokenCounter space;

    public CounterWithWait(long startTime, TokenCounter word, TokenCounter space) {
        this.startTime = startTime;
        this.word = word;
        this.space = space;
    }

    public String info(String info,final long currentTime){
        System.out.println(String.format("thread %s : time milisecond %d",info,currentTime - this.startTime));
        return info;
    }

    public boolean start() throws InterruptedException {

        Thread threadWord = new Thread(word.new TokenThread());
        Thread threadSpace = new Thread(space.new TokenThread());

        this.info(Thread.currentThread().getName() + " start",System.currentTimeMillis());

        threadSpace.start();
        threadWord.start();

        while(threadSpace.isAlive() || threadWord.isAlive()){
             if(((System.currentTimeMillis() - this.startTime) > 1000) && (threadSpace.isAlive() || threadWord.isAlive())){
                 this.info(Thread.currentThread().getName() + " tired of waiting",System.currentTimeMillis());
                 if(threadSpace.isAlive()){
                     threadSpace.interrupt();
                     if(threadSpace.isInterrupted()){
                         throw new InterruptedException();
                     }
                 }
                 if(threadWord.isAlive()){
                     threadWord.interrupt();
                     if(threadWord.isInterrupted()){
                         throw new InterruptedException();
                     }
                 }
             }
        }
        this.info(Thread.currentThread().getName() + " finally job",System.currentTimeMillis());
        return true;
    }

    public static void main(String[] args) throws InterruptedException {
        if(args.length > 0){
            CounterValuesText obj = new CounterValuesText(args[0]);
            String text = obj.readTxtFile(args[1]);
            CounterWithWait counter = new CounterWithWait(System.currentTimeMillis(),new TokenCounter(false,text),new TokenCounter(true,text));
            counter.start();
        }
    }

}

@@ -880,32 +880,54 @@
       this.info(
+String.format(%22%25s %25s%22,
 Thread.currentTh
@@ -946,19 +946,17 @@
 me()
- + 
+,
 %22 start%22
 ,Sys
@@ -951,16 +951,17 @@
 %22 start%22
+)
 ,System.
@@ -1030,18 +1030,73 @@
 read
-Word.start
+Space.join();%0A        threadWord.start();%0A        threadWord.join
 ();%0A
@@ -1301,32 +1301,49 @@
       this.info(
+String.format(%22%22,
 Thread.currentTh
@@ -1358,19 +1358,17 @@
 etName()
- + 
+,
 %22 tired 
@@ -1378,16 +1378,17 @@
 waiting%22
+)
 ,System.
@@ -1939,16 +1939,39 @@
 is.info(
+String.format(%22%25s %25s %22,
 Thread.c
@@ -1998,11 +1998,9 @@
 me()
- + 
+,
 %22 fi
@@ -2009,16 +2009,17 @@
 lly job%22
+)
 ,System.

update threads