producer consumer pattern with concurrenthashmap in java -
i have following problem, , not sure how design parts of solution:
i have large text file read line line. need process each line , update hashmap.
afaik need 1 producer thread read lines file, , dispatch lines pool of consumer threads. consumer threads should update concurrenthashmap , new lines.
my questions are: how can consumer threads access concurrenthashmap? if use fixed thread pool, producer need add line queue first, or can submit or execute new consumer?
edit: zim-zam correct; want consumers dump results concurrenthashmap when finish.
i create concurrenthashmap in main thread, , pass references consumers in constructors. consumers should either add or increment atomicinteger in run methods. how can tell in main thread when of lines read , consumers finished?
thanks again.
you can either have of consumers share same queue producer adds to, or else can give each consumer own queue producer accesses via circular linked list or similar data structure each consumer's queue receives more or less same amount of data (e.g. if have 3 consumers, producer add data queue1, queue2, queue3, queue1, etc).
you can give each consumer reference same concurrenthashmap
(e.g. in consumer's constructor), or else can make concurrenthashmap
accessible via static getter method.
Comments
Post a Comment