CyclicBarrier and Thread for optimize for element for tags in Java How can I use a CyclicBarrier with N-THREAD according to my core for optimize this code?

My code has an input like this:


i 3
a duna automobile deserto -1 
a nissan auto automobile -1 
s aut -1

i 2
a pesca sport frutta -1
s sport -1

END




i = 3 means that there are 3 lines.
"a" means that I'm adding an element (first occurrence after "a") and the other words after the element are my "tags".
"s" means that I have to find the tags that have as substring the word "aut" for the first block and "sport" for the second block.


At the end the output is like this:


2

1




"2" means that I have two elements "duna" &amp; "nissan" that have tags (auto, automobile, auto) that start with the word "aut". 
"1" means that I have the element "pesca" that has tag (sport) that starts with the sub-tag "sport".


With END, the program is finished.

package Prove;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

class FinalMap {


    public static void main(String args[]) throws IOException {


        HashMap&lt;String, Set&lt;String&gt;&gt; map = new HashMap&lt;String, Set&lt;String&gt;&gt;();
        java.io.InputStreamReader isr = new java.io.InputStreamReader(System.in);
        java.io.BufferedReader stdin = new java.io.BufferedReader(isr, 16 * 1024);

        String line = stdin.readLine();
        final String ADD_COMMAND = "a";
        final String ITEM_COMMAND = "i";
        final String SEARCH_COMMAND = "s";
        final String END_MARKER = "&lt;END&gt;";


        while (!line.equals("&lt;END&gt;")) {

            String elemento = "" ;


            if (line.startsWith(ITEM_COMMAND)) 
            {
                map = new HashMap&lt;String, Set&lt;String&gt;&gt;();
            }



            else if (line.startsWith(ADD_COMMAND)) {

                String[] parts = line.split("\\s+");

                for (int i = 2; i &lt; parts.length - 1; ++i) {
                    String element = parts[1];
                    String tag = parts[i];
                    for (int j = 1; j &lt;= parts[i].length(); ++j) {
                        String tagPrefix = tag.substring(0, j);
                        Set&lt;String&gt; elements = map.get(tagPrefix);
                        if (elements == null) {
                            elements = new HashSet&lt;String&gt;();
                            map.put(tagPrefix, elements);
                        }
                        elements.add(element);
                    }
                }


            }


            else if (line.startsWith(SEARCH_COMMAND)) {
                String subtag = line.substring(2,line.length()-3);
                if (map.containsKey(subtag)) {
                    System.out.println(map.get(subtag).size());
                } else {
                    System.out.println("missing");
                }
            }
           line = stdin.readLine();
        }
        stdin.close();
    }
}
String elemento = "" ;


This variable is unused.

Additionally I'd clean up the blank lines you have, it's dangerous to have an if statement end, and then a couple blank lines later, there's an else that continues the if.

            for (int i = 2; i &lt; parts.length - 1; ++i) {
                String element = parts[1];
                String tag = parts[i];
                for (int j = 1; j &lt;= parts[i].length(); ++j) {
                    String tagPrefix = tag.substring(0, j);
                    Set&lt;String&gt; elements = map.get(tagPrefix);
                    if (elements == null) {
                        elements = new HashSet&lt;String&gt;();
                        map.put(tagPrefix, elements);
                    }
                    elements.add(element);
                }
            }


There's a performance optimization here; parts[i].length() is not free. Use final int tagLength = parts[i].length(), then test against that in your for-loop condition.