0

Problems in Details

Will it cause any issue when using data structure e.g ArrayBlockingQueue<ArrayList<MyClass>>
with multiple threads?

Background

In a high level, i am trying to achieve that I have one producer which will produce a giant list. In order to speed up the processing. I decide to use multiple consumers(threads) consuming the giant list produced by the producer.

My Proposal Solution

I will be converting the giant list to multiple relatively smaller list and in order to ensure its thread safe, I will enqueue these smaller lists to a concurrent data structure. So in multi-threads scenario, each thread just poll the concurrent queue to get one list and work on it.

Problem Statement

In multi-threads scenario, I understand we have to use the concurrent data structure to avoid thread interference and build happen-before relation.

  • But will it be safe that using non-thread-safe data structure as element of thread-safe data structure?

  • Will it cause any issue when using data structure e.g ArrayBlockingQueue<ArrayList<MyClass>>
    with multiple threads?

  • Will it be any impact to the performance?
Huazhe Yin
  • 355
  • 1
  • 4
  • 19
  • any help/suggestion are welcomed! – Huazhe Yin Jun 02 '19 at 22:53
  • 1
    Have you considered enqueuing the individual `MyClass` elements one by one? Maybe your producer does not need to build up the huge list in the first place that way? – Thilo Jun 03 '19 at 07:00
  • @Thilo .thanks for coming up with this solution. To my use case, i will only have limited number of threads as consumer. so even with enqueuing the individual ` MyClass ` , will it bring down the performance? Concurrent data structure internally is using reentrant lock, it may be possible that one thread is blocking another thread for a brief time. So if i use ` ConcurrentQueue>`, then i can reduce the interaction of between different threads and the shared variable. – Huazhe Yin Jun 03 '19 at 16:45

2 Answers2

2

There shouldn't be an obvious problem with this approach.

will it be safe that using non-thread-safe data structure as element of thread-safe data structure?

This is safe as long as you properly coordinate (or avoid) concurrent access to the non-thread-safe inner data structure. The ArrayBlockingQueue ensures the happens-before relation is established when you access its elements via on of the peek, poll or related methods.

Will it cause any issue when using data structure e.g ArrayBlockingQueue<ArrayList<MyClass>> with multiple threads?

No, this is what BlockingQueue is intended for as long as you coordinate access to the inner lists (see above).

Will it be any impact to the performance?

In general the approach where the single producer partitions the list into sub-lists might not be optimal. The producer does not / should not know about the number of consumers and their bandwidth and thus in general does not know what partition sizes work well. A better approach might be to use an ArrayBlockingQueue<MyClass> and from the consumer side always consume multiple elements in one go by calling drainTo for a suitable number maxElements of elements.

michid
  • 10,536
  • 3
  • 32
  • 59
  • Thx for ur comments. so the short answer is that 1. It will not be any issue if I use drainTo to a new list and start working on it. 2. Performance will not be affected at all, since each thread has their own list as they are working on a single thread process. do i understand it correctly? – Huazhe Yin Jun 03 '19 at 16:46
  • Thanks. I will update my question once I completed this project. :) – Huazhe Yin Jun 07 '19 at 19:01
1

Thanks the Answer from michid@ and Thilo@

Final Resolution

I end up using LinkedBlockingQueue<List<MyObjClass>> and have multiple child threads polling from the queue. Each child thread will take list of MyObjClass to work on.

This resolution does not have any impact to slow down the performance.
For why i am choosing LinkedBlockingQueue over ArrayBlockingQueue see Link

Huazhe Yin
  • 355
  • 1
  • 4
  • 19