Assume that the task is like this: for a given string, such as "threadTest", give it another "@" and return the result. There is no dependency between these tasks.
Assume that there are many such strings, such as 1000, and the number of threads that can be created cannot exceed 35.
As a result, after the thread is created, how can we elegantly allocate a given task group to the thread pool in the code, and then collect the processing results of these threads?
I want to use the Future in java. util. concurrency, but it seems that it does not support collecting batch results?
As a result, the thread writes a code that roughly allocates tasks and collects results, which is ugly. How can we improve and design?
Below is my rough implementation code ==========
Java code
Package rmn. thread;
Import java. util. ArrayList;
Import java. util. Arrays;
Import java. util. HashMap;
Import java. util. List;
Import java. util. Map;
Public class TaskAssignAndResultCollection {
Private final static int DEFAULT_THREAD_NUM = 5;
Private int threadNum = DEFAULT_THREAD_NUM;
Private Worker [] threads = null;
Public TaskAssignAndResultCollection (int threadNum ){
Super ();
If (threadNum = 0 ){
ThreadNum = DEFAULT_THREAD_NUM;
} Else {
This. threadNum = threadNum;
}
}
Public Map <String, String> processStringBatchly (
String [] datas ){
If (threads = null ){
Synchronized (this ){
Threads = new Worker [threadNum];
For (int I = 0; I <threadNum; I ++ ){
Threads [I] = new Worker ();
}
}
}
// How do I allocate the domainName to the thread and let them run it on their own? Average allocation,
Int domainSize = datas. length;
Int domainNamePerThread = domainSize/threadNum;
Int leftDomainName = domainSize % threadNum;
List <String> listDomainName = Arrays. asList (datas );
// First, each thread is evenly divided into domainNamePerThread DomainName,
Int endIndex = 0;
For (int I = 0; I <threadNum; I ++ ){
Int beginIndex = I * domainNamePerThread;
Int step = domainNamePerThread;
EndIndex = beginIndex + step;
List <String> subDomainNames = new ArrayList <String> (listDomainName. subList (beginIndex, endIndex ));
Threads [I]. setDomainNameList (subDomainNames );
}
// Then, allocate the remaining parts one by one.
For (int I = 0; I <leftDomainName; I ++ ){
Threads [I]. addDomainName (listDomainName. get (endIndex + I ));
}
For (Worker thread: threads ){
Thread. start ();
Try {
Thread. join ();
} Catch (InterruptedException e ){
E. printStackTrace ();
}
}
Map <String, String> totalResult = new HashMap <String, String> ();
For (Worker thread: threads ){
TotalResult. putAll (thread. getResultCollector ());
}
Return totalResult;
}
Public static void main (String [] args ){
String [] datas = new String [] {"baidu.com", "sohu.com", "163.com"," iteye.com "};
TaskAssignAndResultCollection c = new TaskAssignAndResultCollection (3 );
Map <String, String> resultCollector = c. processStringBatchly (datas );
C. showMsg (resultCollector );
}
Private void showMsg (Map <String, String> result ){
For (Map. Entry <String, String> me: result. entrySet ()){
String data = me. getKey ();
String r = me. getValue ();
String msg = "original value [" + data + "]" + "after processing [" + r + "]";
System. out. println (msg );
}
}
}
Class Worker extends Thread {
Private List <String> datas;
Private Map <String, String> resultCollector = new HashMap <String, String> ();
Public void run (){
For (String d: datas ){
String result = d + "@";
ResultCollector. put (d, result );
}
}
Public void setDomainNameList (List <String> subDomainNames ){
Datas = subDomainNames;
}
Public void addDomainName (String domainName ){
If (datas = null ){
Datas = new ArrayList <String> ();
}
Datas. add (domainName );
}
Public Map <String, String> getResultCollector (){
Return resultCollector;
}
}