I have a problem in queue threads with ThreadPoolExecutor
Sometimes it overlaps the same task.
Here is my code:
int threadSize = 3;
int maxThreadSize = 3;
int TTL = 10;
int queueSize = map.size();
ArrayBlockingQueue<Runnable> queue = new ArrayBlockingQueue<Runnable>(queueSize);
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(threadSize, maxThreadSize, TTL, TimeUnit.SECONDS, queue, new ThreadPoolExecutor.CallerRunsPolicy());
for(final Entry<String,String> entry : map.entrySet()){
threadPool.execute(new Runnable() {
@Override
public void run() {
//…do something
System.out.println(entry.getKey + " : " + entry.getValue);
}
});
}
example
If map
have a pair of key and value like shown below.
Key : Values
"1" : "AAA"
"2" : "BBB"
...
"26" : "ZZZ"
My result sometimes can be…
1 : AAA
2 : BBB
3 : CCC
3 : CCC
4 : DDD
...
25 : YYY
It's double "3 : CCC" and have no "26 : ZZZ". How can I solve this problem?
[Edit] this problem happen randomly not only "3 : CCC"
sometimes it happen to other entry
sometimes the problem doesn't happen
when one entry has overlapped is mean one other will not be printed
so I guess it is my queing problem