Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multithreading/Parallelizing each item of for loop JAVA

I am trying to have each thread access a single item of for loop while another thread accesses the next item. I want to do this using multiple threads and number of multiple threads created will be input by the user. I have done this using executorservice and streams. I want to do this using simple threads. Is the below correct? Is there a better way?

Map<String, String> fileMap = new HashMap<>();
fileMap.put("Age", "Age is not remotely associated with it.");
fileMap.put("Gender", "Gender plays a role but not that important.");
fileMap.put("Money", "People do not believe but this is the only factor that matters.");

Runnable myRunnable = new Runnable(){
    public void run(){
        for (Map.Entry<String, String> entry : fileMap.entrySet()) {
            synchronized(this){
                int counter = 0;
                Pattern p = Pattern.compile("not");
                Matcher m = p.matcher(entry.getValue());
                while (m.find()) {
                    counter++;
                }
                System.out.println("File Name: " + entry.getKey());
                System.out.println("Count: " + counter);
                System.out.println(Thread.currentThread().getName());
            }
        }
    }    
};

int n = Integer.parseInt(args[0]);
for (int x=0; x<n; x++)
{
    Thread temp= new Thread(myRunnable, "Thread #" + x);
    temp.start();
    System.out.println("Started Thread:" + x);
}

Also, is it possible to have a thread not to go back to previous item since a previous thread has already computed the value? Any help would be appreciated. Thanks

like image 213
Tao Avatar asked Sep 15 '25 05:09

Tao


1 Answers

Here is a solution to your problem. This parses the thread name to provide the index and uses final arrays to handle passing data into the threads.

Map<String, String> fileMap = new HashMap<>();
fileMap.put("Age", "Age is not remotely associated with it.");
fileMap.put("Gender", "Gender plays a role but not that important.");
fileMap.put("Money", "People do not believe but this is the only factor that matters.");


final int[] tgSize = new int[]{0};
final Map.Entry[][] entryArr = new Map.Entry[1][];

Runnable myRunnable = new Runnable(){
    public void run(){
        Integer index = Integer.valueOf(Thread.currentThread().getName().substring(8));

        for(int i = index; i < fileMap.size(); i += tgSize[0]) {
            int counter = 0;
            @SuppressWarnings("unchecked")
            Map.Entry<String, String> entry = entryArr[0][i];
            Pattern p = Pattern.compile("not");
            Matcher m = p.matcher(entry.getValue());
            while (m.find()) {
                counter++;
            }
            synchronized(this) {
                System.out.println("File Name: " + entry.getKey());
                System.out.println("Count: " + counter);
                System.out.println(Thread.currentThread().getName());            
            }
        }
    }    
};

int n = Integer.parseInt(args[0]);

tgSize[0] = n < fileMap.size() ? n : fileMap.size();
entryArr[0] = fileMap.entrySet().toArray(new Map.Entry[fileMap.size()]);


for (int x=0; x<n && x < fileMap.size(); x++)
{
    Thread temp= new Thread(myRunnable, "Thread #" + x);
    temp.start();
    System.out.println("Started Thread:" + x);
}
like image 188
Ralph Ritoch Avatar answered Sep 16 '25 18:09

Ralph Ritoch