4
4
4ainik2017-01-05 12:49:26
Java
4ainik, 2017-01-05 12:49:26

How to ensure thread interaction in android?

It is planned to make 2 streams. In the first is the accumulation of information in the buffer. In the second processing of the accumulated information. And so there was a question how in due time to notify the second flow that the buffer is full?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-01-09
@fantomasdnb

I think there may be several solutions.
1 - synchronized blocks with wait/notify[All]. The handler got up to wait, the drive plows as soon as it finished, gets up and does notifyAll and gets up to sleep. further similarly.
Problems: it is difficult to scale, notifyAll will wake up all waiting threads and if more is added, then everyone will wake up and everyone will eat the buffer.
2 - Similar solution with Locks.
3 - Queue, One thread writes to the queue, the other reads from it, for sure you can wrap the buffered stream in the queue or wrap your buffer around this queue. Pros - easy to implement, synchronization out of the box. Cons - scaling - it is not clear how to read chunks from the buffer from the queue by multiple streams, in order to then give the result in the right order.
4 - pool of handler threads. This solution is needed for easier scaling, it may not work for you. You make a thread pool that can handle a specific buffer class. The ready accumulator feeds the buffer to a ready thread from the pool simply by starting this thread.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question