简体   繁体   中英

Java Concurrency: Count characters of String

while doing some Java practice, I came upon a task including multithreading and concurrency. I have absolutely no experience with that so far. The foundation is a java class/program, which has a function to count characters in a string. The function is provided with the string and a ConcurrentHashMap with the alphabet in lowercase (each char as key) and the occurence of each char as value (as integer). The program works (with Hashmap and no multithreading, meaning without implementing runnable and without the public void run).

I understand the usage of ConcurrentHashMap in order to enable multithreading and thus implemented ConcurrentHashMap (using it instead of HashMap). Furthermore I know, that my class needs to implement runnable, and thus has a public void run () method.

My aim:

I would like to know, how to start three threads, which all count the character's occurences of the same String and write it into the ConcurrentHashMap.

Am I right, that one utilizes this kind of implementation to make the program run faster? (answered)

Further information

As understood from the answers, it is not clear, why one would do this. It is a practice task. Later on, I might add file-input of large text-files (or might not, it is practice, I do not know).

Final edit

So, it is not useful for this purpose to do multithreading. No further answers needed.

My Code so far:

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

class WordCounter implements Runnable {

    // method to count characters in given string
    static void characterCount(String inputString, ConcurrentHashMap<Character, Integer> charCountMap) {
        // Converting String to lowercase
        inputString = inputString.toLowerCase();

        // Converting given string to char array
        char[] strArray = inputString.toCharArray();

            // checking each char of strArray
            for (char c : strArray) {
                if (charCountMap.containsKey(c)) {
                    // If char is present in charCountMap,
                    // incrementing it's count by 1
                    charCountMap.put(c, charCountMap.get(c) + 1);
                }
            }

            // Printing the charCountMap
            for (Map.Entry entry : charCountMap.entrySet()) {
                if(!entry.getValue().equals(0)){
                System.out.println(entry.getKey() + " " + entry.getValue());
            }}

    }


    // Main
    public static void main(String[] args)
    {
        // Creating a HashMap containing alphabet in lower case
        // as a key and occurrences as  a value (initialized with value: 0)
        ConcurrentHashMap<Character, Integer> charCountMap
                = new ConcurrentHashMap<>();
        for (char ch = 'a'; ch <= 'z'; ++ch)
            charCountMap.put(ch, 0);

        String str = "GGACACGTagGcGT";
        characterCount(str, charCountMap);
    }

    @Override
    public void run() {

    }
}

The one question I saw in your question:

Am I right, that one utilizes this kind of implementation to make the program run faster?

No, not in this case. You have to understand that creating and starting threads, and then synchronizing them somehow to avoid race conditions (to achieve correct, deterministic behavior) doesn't come for free.

Threads are resources of the underlying operating system. It takes time to create, start, manage them.

Therefore: using multiple threads does not automatically translate into "my program runs faster". Multiple threads only make things "faster" when the advantage of processing data in parallel gains more than the initial cost of creating these threads. And of course, you also need hardware able to run threads in parallel. If your hardware would be able to run only one thread at time, then doing stuff that only uses the CPU (and never waits for some external input) then having multiple threads would always be slower.

Now, your task is to count the characters in a short string, provided by a human user. This is most quickly solved by a single thread iterating the string, and doing its work. Thus: most likely, your multi threaded program will be quite a bit slower compared to a straight forward single threaded solution.

If, on the other hand your task would be to read thousands of files, with millions of lines of text, for example to build some sort of index for a full text search, then of course: using multiple threads can dramatically speed up overall execution time.

Beyond that: the code you have written so far does nothing. To make a reasonable program you would need to:

  • have some code in that run() method.
  • to then create multiple threads that invoke that run() method in parallel

Of course, that also requires you to reasonable partition your data. For example, you could have each thread count a specific sub string of your input.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM