package ru.job4j.threads;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * WordsAndSpaces.
 *
 * @author Alexander Ivanov
 * @since 30.04.2017
 * @version 1.0
 */
public class WordsAndSpaces {
    /**
     * Number of words.
     */
    private int nWords = 0;

    /**
     * Number of spaces.
     */
    private int nSpaces = 0;

    /**
     * Variable for stopping threads.
     */
    private boolean programStop = false;

    /**
     * Count words and spaces in text by two threads.
     * @param s - text.
     */
    public void numberOfWordsAndSpaces(String s) {
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                long startTime = System.currentTimeMillis();
                String regEx = "\\w+";
                Pattern pattern = Pattern.compile(regEx);
                Matcher matcher = pattern.matcher(s);
                while (!Thread.currentThread().isInterrupted() && matcher.find()) {
                    nWords++;
                    System.out.println("Counted: " + nWords + " words");
                    if (System.currentTimeMillis() - startTime > 1000 || programStop) {
                        Thread.currentThread().interrupt();
                    }
                }
                System.out.println("Number of words: " + nWords);
            }
        });

        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                long startTime = System.currentTimeMillis();
                String regEx = "\\s+";
                Pattern pattern = Pattern.compile(regEx);
                Matcher matcher = pattern.matcher(s);
                while (!Thread.currentThread().isInterrupted() && matcher.find()) {
                    nSpaces++;
                    System.out.println("Counted: " + nSpaces + " spaces");
                    if (System.currentTimeMillis() - startTime > 1000 || programStop) {
                        Thread.currentThread().interrupt();
                    }
                }
                System.out.println("Number of spaces: " + nSpaces);
            }
        });

        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * Method for program stopping counting words and spaces.
     */
    public void stopCounting() {
        this.programStop = true;
    }

    /**
     * Main method.
     * @param args for cmd.
     */
    public static void main(String[] args) {
        System.out.println("Program is starting.......");
        WordsAndSpaces wordsAndSpaces = new WordsAndSpaces();
        wordsAndSpaces.numberOfWordsAndSpaces("Number of words should be twelve. "
                + "Number of spaces should be eleven");
        System.out.println("...............End");
    }
}

@@ -774,143 +774,270 @@
     
-String regEx = %22%5C%5Cw+%22;%0A                Pattern pattern = Pattern.compile(regEx);%0A                Matcher matcher = pattern.matcher(s);%0A
+char%5B%5D array = s.toCharArray();%0A                boolean isWord = false;%0A                for (int i = 0; i %3C array.length && !Thread.currentThread().isInterrupted(); i++) %7B%0A                    if (System.currentTimeMillis() - startTime %3E 1000 %7C%7C programStop) %7B%0A  
     
@@ -1044,32 +1044,30 @@
             
-while (!
+      
 Thread.curre
@@ -1074,34 +1074,32 @@
 ntThread().i
-sI
 nterrupt
 ed() && matc
@@ -1090,34 +1090,253 @@
 rupt
-ed() && matcher.find()) %7B%0A
+();%0A                        break;%0A                    %7D%0A                    if (Character.isLetter(array%5Bi%5D)) %7B%0A                        isWord = true;%0A                    %7D%0A                    if (!Character.isLetter(array%5Bi%5D) && isWord) %7B%0A    
     
@@ -1357,24 +1357,68 @@
   nWords++;%0A
+                        isWord = false;%0A    
             
@@ -1502,133 +1502,177 @@
     
-if (System.currentTimeMillis() - startTime %3E 1000 %7C%7C programStop) %7B%0A                        Thread.currentThread().interrupt(
+%7D%0A                    if (i == array.length - 1 && isWord) %7B%0A                        nWords++;%0A                        System.out.println(%22Counted: %22 + nWords + %22 words%22
 );%0A 
@@ -1986,142 +1986,227 @@
     
-String regEx = %22%5C%5Cs+%22;%0A                Pattern pattern = Pattern.compile(regEx);%0A                Matcher matcher = pattern.matcher(s);
+char%5B%5D array = s.toCharArray();%0A                for (int i = 0; i %3C array.length && !Thread.currentThread().isInterrupted(); i++) %7B%0A                    if (System.currentTimeMillis() - startTime %3E 1000 %7C%7C programStop) %7B
 %0A   
@@ -2222,16 +2222,16 @@
     
-while (!
+        
 Thre
@@ -2246,34 +2246,32 @@
 ntThread().i
-sI
 nterrupt
 ed() && matc
@@ -2262,34 +2262,14 @@
 rupt
-ed() && matcher.find()) %7B%0A
+();%0A  
     
@@ -2288,17 +2288,15 @@
     
-nSpaces++
+  break
 ;%0A  
@@ -2317,208 +2317,163 @@
     
-System.out.println(%22Counted: %22 + nSpaces + %22 spaces%22);%0A                    if (System.currentTimeMillis() - startTime %3E 1000 %7C%7C programStop) %7B%0A                        Thread.currentThread().interrupt(
+%7D%0A                    if (array%5Bi%5D == 32) %7B%0A                        nSpaces++;%0A                        System.out.println(%22Counted: %22 + nSpaces + %22 spaces%22
 );%0A 

Add task_1_Threads_#2 and #3.