Would an inner static runnable in a singleton block if used as wrapper for callables, if it modifies a singleton's static param?

StackOverflow https://stackoverflow.com/questions/18338651

Domanda

1) Is there a chance that an inner static Runnable class can block if it modifies a static ConcurrentHashMap in the outer singleton, after it calls a Callable? I'm thinking about a scenario where the modification is made to the same item in the map by multiple runnables at the same time. Multiple runnables are ran in a static ThreadPoolExecutor in the outer singleton.

2) Would the callable be called in the same Thread as the runnable, if i do a val = myCallable.call()? It modifies the concurrenthashmap depending on the result of a callable.

È stato utile?

Soluzione

  1. ConcurrentHashMap is designed to be non-blocking, although some contention is possible if multiple updates to the same hash segment happened simultaneously.
  2. Yes.

Altri suggerimenti

  1. Except when running static initializers, Java will never implicitly block.

  2. call() is a normal method call and will run synchronously in the calling thread, like any other method call.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top