java - ConcurrencyHashMap and BashProcessing -


first abstraction. problem can modelized following :

i have room n doors, , can drop package in room. once there given number of package in room, want them shipped away while keeping doors open.

using java 1.8, i'm working on multi-threaded application thread can add items concurrenthashmap object.

i want regularly dump concurrenthashmap when reaches size without blocking threads adding items map. dumping including several operations costly.

i thought of following solutions :

  • check size of hashmap each time add something, , if map reached max size it'll copy other map, reset , continue. not sure it'll thread safe

  • create wrapper function put() method of concurrenthashmap synchronized. believe i'll loose advantage of using concurrenthashmap

  • use arraylistblockingqueue batch size size. it'll block when full, i'll need process later.

  • something else didn't think of.

i self taught regarding java threads , i'm looking suggestions , ways tackle problem.

i still don't understand doors analogy. sounds me need blockingqueue:

  • "somebody dropping package in room" call queue.offer(obj)
  • "shipping items away" consumer thread taking s items queue, , doing objects:

    while (true) {   object[] objs = new object[s];   (int = 0; < s; ++i) {     objs[i] = queue.take();  // perhaps timeout?   }   dosomethingwithobjects(objs); } 

in way, can keep on offering items queue ("keeping doors open") while consumer thread processing them (provided create queue sufficient capacity).


Comments