Java?????????
???????????? ???????[ 2015/3/19 14:30:53 ] ????????Java ??????? ????? ???? ???
??????????????????????????????????о??????????????????Ч????????????????????????????????????????????????
????1.????????????????????????dataList(?????dataList???)
????1.1???????????????????????????????????????
????2.?? int threadNum = Runtime.getRuntime().availableProcessors();??????cpu?????
????2.1????????????????????????????????????????????????Щ???????cpu??????????
??????Щ?????????Щ??????????????????????ж?????????????????????????????????????????????Ч????(????????λ????????IBM???????????????)????????????????????????£????????? 4??????????????????????????
????3. ????dataListSize ?? threadNum????????????????????????????
????3.1 ???????
int threadListSize = dataListSize;
if(listSize<threadNum)
{
threadNum=1;
}
else if(threadNum>4)
{
threadNum=4;
threadListSize = listSize / threadNum;
}
else
{
threadListSize = listSize / threadNum;
}
final int[] dataRange = new int[threadNum];
for (int i = 0; i < threadNum - 1; i++) {
dataRange[i] = threadListSize * (i + 1);
}
//??Щ???????????????????????dataRange?????????????е?
dataRange[threadNum - 1] = listSize;
????3.2?Щ???
????????????????????????????????????????????????? dataList ?????????????????????????????????????cpu?????(threadNum)???????????? dataList?????? ????????
???????????????????????????list??????????????????list.get(0)????????????????????????????dataList??????
????4.??????????
????4.1 ??????
????4.1.1???????
ExecutorService executor = Executors.newFixedThreadPool(threadNum);
Future<JSONArray>[] results = new Future[threadNum];
for (int i = 0; i < threadNum; i++) {
int startIndex=0;
if(i!=0)
{
startIndex=dataRange[i-1];
}
results[i]=executor.submit(new CounterTask(startIndex?? dataRange[i]));
}
//????????????????????????????????? Future<JSONArray>[]
JSONArray resultArray=new JSONArray();
for(int i = 0; i < threadNum; i++)
{
resultArray.addAll(results[i].get());
}
//??????????
executor.shutdown();
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11