package com.greensnow25;

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

/**
 * public class WordsSpacesCounting.
 *
 * @author greensnow25.
 * @version 1.
 * @since 08.06.2017.
 */
public class WordsSpacesCounting {
    /**
     * Analysis string.
     */
    private String line;
    /**
     * separator.
     */
    private final String separator = System.getProperty("line.separator");

    /**
     * constructor.
     *
     * @param line Analysis string.
     */
    public WordsSpacesCounting(String line) {
        this.line = line;
    }

    /**
     * Method counts words.
     *
     * @return number of words.
     */
    public int countWords() {
        int count = 0;
        Pattern pattern = Pattern.compile("\\w*[^\\s*]");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            count++;
        }
        return count;
    }

    /**
     * Method counts spaces.
     * @return number of spaces.
     */
    public int countSpaces() {
        int count = 0;
        Pattern pattern = Pattern.compile("\\s");
        Matcher matcher = pattern.matcher(line);
        while (matcher.find()) {
            count++;
        }
        return count;
    }

    /**
     * Method run threads.
     */
    public void runThreads() {
        new Thread() {
            @Override
            public void run() {
                System.out.printf("%s %d%s", "Number of words", countWords(), separator);
            }
        }.start();
        new Thread() {
            @Override
            public void run() {
                System.out.printf("%s %d%s", "Number of spaces", countSpaces(), separator);
            }
        }.start();
    }

    /**
     * psvm.
     * @param args args.
     */
    public static void main(String[] args) {
        WordsSpacesCounting word = new WordsSpacesCounting("wdwed ewf ewf wefwe f ef dsf sd f we2 7");
        for (int i = 0; i != 5; i++) {
            word.runThreads();
        }

    }
}

@@ -403,16 +403,90 @@
 tor%22);%0A%0A
+    private Thread one;%0A%0A    private Thread two;%0A%0A    private long wait;%0A%0A
     /**%0A
@@ -624,24 +624,50 @@
 ine = line;%0A
+        this.wait = 1000;%0A
     %7D%0A%0A    /
@@ -1030,24 +1030,31 @@
 nts spaces.%0A
+     *%0A
      * @retu
@@ -1406,34 +1406,58 @@
 ) %7B%0A        
-new Thread
+this.one = new Thread(new Runnable
 () %7B%0A       
@@ -1495,32 +1495,121 @@
 ic void run() %7B%0A
+                if (one.isInterrupted()) %7B%0A                    return;%0A                %7D%0A
                 
@@ -1697,32 +1697,46 @@
      %7D%0A        %7D
+);%0A        one
 .start();%0A      
@@ -1737,26 +1737,50 @@
         
-new Thread
+this.two = new Thread(new Runnable
 () %7B%0A   
@@ -1822,32 +1822,121 @@
 ic void run() %7B%0A
+                if (two.isInterrupted()) %7B%0A                    return;%0A                %7D%0A
                 
@@ -2034,25 +2034,269 @@
        %7D
-.start();
+);%0A        this.two.start();%0A%0A        try %7B%0A            one.join(this.wait);%0A            two.join(this.wait);%0A            one.interrupt();%0A            two.interrupt();%0A        %7D catch (InterruptedException e) %7B%0A            e.printStackTrace();%0A        %7D
 %0A    %7D%0A%0A
@@ -2316,16 +2316,23 @@
 * psvm.%0A
+     *%0A
      * @
@@ -2398,16 +2398,44 @@
 %5B%5D args)
+ throws InterruptedException
  %7B%0A     
@@ -2587,24 +2587,137 @@
     
-word.runThreads(
+System.out.println(%22start%22);%0A            word.runThreads();%0A%0A            word.two.join();%0A            System.out.println(%22finish%22
 );%0A 

Update date 13.06.17