2

I'm just looking the pattern for high performance for the following scenario :

We've N threads that are doing some processing, during this processing it's possible that we need to execute a 'special'code that has been encapsulated in a Java class. The objects are not thread safe, but I can have M of those to be executed in parallel.

So from N threads I'm calling a pool of M objects.

   thread 1 :
      doing thinks
      PoolObject -> Object1.execute()   // no synchronize

   thread 2 :
      doing thinks
      PoolObject -> Object2.execute()   // no synchronize

   thread n :
      doing thinks
      PoolObject -> wait object is available
      PoolObject -> Object2.execute()

ArrayBlockingQueue is an implementation that could be used but it's using a ReentrantLock ( aka synchronize ). It's a solution like ConcurrentHashMap that uses compareAndSwapLong like mechanism.

Is there some existing code for executing this in high performance scenario like ConcurrentHashMap ?

ic3
  • 7,917
  • 14
  • 67
  • 115
  • You cannot safely / efficiently wait for something to happen in Java without some form of synchronization. So ... what you are asking for cannot exist. OTOH, you are probably mistaken about the overheads of synchronization. It might help if your example was better explained, but I suspect that the answer would still be "impossible". – Stephen C Apr 17 '17 at 08:13
  • check how ConcurrentHashMap works (hint compareAndSwapLong ) ;-) – ic3 Apr 17 '17 at 08:20
  • If you would use a Concurrent List or something as a data struture for the pool, than that IS your synchronization mechanism. – NickL Apr 17 '17 at 08:25
  • I've changed synchronization to synchronize to stress I dont want to use 'synchronize'. – ic3 Apr 17 '17 at 08:36
  • @ic3 - 1) compareAndSwap is a form of sychronization primitive. (Duh!) 2) I still think you are barking up the wrong tree here. This *looks to me* like a classic example of premature optimization; i.e. wasting time on effort on optimizing at design time ... when the chances are that this (e.g. `synchronize`) won't be the real performance bottleneck. 3) There are various standard data structures that have been optimized for high levels of concurrency; e.g. the `ConcurrentXXX` classes. – Stephen C Apr 17 '17 at 09:09
  • 2
    4) If a ConcurrentXXX class does not exist, that is a sign that a "lock free" implementation is impractical (!) 5) Your problem description is too vague for us ti make concrete recommendations. – Stephen C Apr 17 '17 at 09:15
  • I've rewrite so it's clear that I mean Lock/synchronize, no need to play with words. https://en.wikipedia.org/wiki/Compare-and-swap it's not a LOCK. I'm asking if it exists that's it. 4) Sorry, it's really an absurd answer – ic3 Apr 17 '17 at 09:38
  • @Stephen, 5) please don't speak on behalf of the community, speak on yours – ic3 Apr 17 '17 at 09:39
  • @ic3 - Good luck then. – Stephen C Apr 17 '17 at 09:59
  • @ic3 take a look at Akka and/or VertX. They both offer compelling high performance threading models. The Actor model reduces blocking by going asynchronous. That is, by sharing multiple threads behind multiple work queues. When one actor has no work to do, it does not cost much if any overhead. When a resource needs to be shared then messages are sent to that resource (actor) to perform work, and a reply will be sent back asynchronously. In your scenario, the shared objects could be their own actors and anybody who needs access to them queues their work with them to happen in sequence. – Chris K Apr 17 '17 at 10:02

1 Answers1

1

Eventually I've found the class, ConcurrentLinkedQueue.

For the ones who are wondering why CAS versus synchronization, you can read this.

Community
  • 1
  • 1
ic3
  • 7,917
  • 14
  • 67
  • 115