Here are two ways for writing this sample code (one using multi-threading, one without using multi-threading) - The original code (friend wrote it) uses multi-threading. I would like to know which method is better, and was there a motivation for using multi-threading in this situation?1) First way (multi-threading):main Class:public class main {    public static void main(String[] args)    {           MyWorker worker1 = new MyWorker();           }}MyWorker Classs:import java.util.HashMap;import java.util.LinkedList;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class MyWorker {    HashMap<String, Integer> HM_1, HM_2, HM_both;    ExecutorService threadExecutor;    LinkedList<String> myStrings_1;    LinkedList<String> myStrings_2;    public MyWorker()    {        myStrings_1 = new LinkedList<String>();        myStrings_1.add(Letter A);        myStrings_1.add(Letter B);        myStrings_1.add(Letter C);        myStrings_2 = new LinkedList<String>();        myStrings_2.add(Letter D);        myStrings_2.add(Letter E);        myStrings_2.add(Letter F);        threadExecutor = Executors.newFixedThreadPool(10);        HM_1= new HashMap<String, Integer>();        HM_2 = new HashMap<String, Integer>();        HM_both = new HashMap<String, Integer>();        for(Object o_1: myStrings_1.toArray())        {             // multi-threading            String f_1 = (String)o_1;            threadExecutor.execute(new Helper(this, f_1, true));        }        for(Object o_2: myStrings_2.toArray())        {             // multi-threading            String f_2 = (String)o_2;            threadExecutor.execute(new Helper(this, f_2, false));        }        threadExecutor.shutdown();    }    public void getWorkerData(String f, boolean b, int currNum)    {        if(b)        {            HM_1.put(f, currNum);            HM_both.put(f, currNum);        }        else        {            HM_2.put(f, currNum);            HM_both.put(f, currNum);        }    }}Helper Class:public class Helper implements Runnable{    public MyWorker currWorker1;    public String s;    public boolean b;    public Helper(MyWorker _currWorker1, String _s, boolean _b)    {        s = _s;        currWorker1 = _currWorker1;        b = _b;    }    public void run()    {        currWorker1.getWorkerData(s, b, 12);    }}2) Second way (no multi-threading):main_noThreading Class:public class main_noThreading{    public static void main(String[] args)    {           MyWorker_noThreading worker1 = new MyWorker_noThreading();           }}MyWorker_noThreading:import java.util.HashMap;import java.util.LinkedList;public class MyWorker_noThreading {    HashMap<String, Integer> HM_1, HM_2, HM_both;    LinkedList<String> myStrings_1;    LinkedList<String> myStrings_2;    public MyWorker_noThreading()    {        myStrings_1 = new LinkedList<String>();        myStrings_1.add(Letter A);        myStrings_1.add(Letter B);        myStrings_1.add(Letter C);        myStrings_2 = new LinkedList<String>();        myStrings_2.add(Letter E);        myStrings_2.add(Letter F);        myStrings_2.add(Letter G);        HM_1 = new HashMap<String, Integer>();        HM_2 = new HashMap<String, Integer>();        HM_both = new HashMap<String, Integer>();        for(Object o_1: myStrings_1.toArray())        {             // no multi-threading            String f_1 = (String)o_1;            new Helper_noThreading(this, f_1, true);        }        for(Object o_2: myStrings_2.toArray())        {             // no multi-threading            String f_2 = (String)o_2;            new Helper_noThreading(this, f_2, false);        }    }    public void getWorkerData(String f, boolean b, int currNum)    {        if(b)        {            HM_1.put(f, currNum);            HM_both.put(f, currNum);        }        else        {            HM_2.put(f, currNum);            HM_both.put(f, currNum);        }    }}MyHelper_noThreading Class:public class Helper_noThreading{    public MyWorker_noThreading currWorker1;    public String s;    public boolean b;    public Helper_noThreading(MyWorker_noThreading _currWorker1, String _s, boolean _b)    {        s = _s;        currWorker1 = _currWorker1;        b = _b;        currWorker1.getWorkerData(s, _b, 12);    }}
What's the motivation for multi-threading in this code?
java;multithreading
I'm only commenting the threading part of the code you posted and regarding those parts I would say that the multithreaded code is not correctly written. None of the data structures written to by the threads are thread safe and no locking mechanism for maintaining thread safety is in place.Performance wise there is also absolutely no reason for multithreading this code. There are no system calls or complex calculations present that might benefit from being run in parallell.As for your question I would say that the non multithreaded solution is better (although you could surely simplify that code quite a bit).