8

Is it possible to implement a queue of background tasks using rxjava? I need possibility to add task at any time and only one tasks executed simultaneously. I have tried PublishSubject, but when i'm pushing execution to the new thread observeOn(Schedulers.newThread()) it starts more than one task at time.

UPDATE: Is it possible to implement something like producer-consumer pattern using rxjava?

MightySeal
  • 2,293
  • 2
  • 17
  • 32
  • MightySeal please hava look at Looper in android http://developer.android.com/reference/android/os/Looper.html – Fasiha Jul 16 '15 at 07:40
  • @Fasiha how will it help? – MightySeal Jul 16 '15 at 07:53
  • pleas see this http://stackoverflow.com/questions/7597742/what-is-the-purpose-of-looper-and-how-to-use-it – Fasiha Jul 16 '15 at 08:24
  • @Fasiha it's not about the solution and not about rxjava. Actually I'm new to rxjava and looking for producer-consumer alike pattern implementation. – MightySeal Jul 16 '15 at 08:26
  • ok so you need something like mutex lock or semaphore? – Fasiha Jul 16 '15 at 08:51
  • @Fasiha nope, it's all about reactive paradigm. – MightySeal Jul 16 '15 at 08:53
  • 6
    RxJava isn't replace-everything-with-it framework. It's just a library. Use `BlockingQueue`, `Executors`, `Services` or what you were using before to create a queue of background tasks and then use `Observable` to describe what operation task is. – Sergii Pechenizkyi Jul 16 '15 at 09:49
  • Did you find the answer? I'm also trying to implement message queue using RxJava – Anton Shkurenko Dec 13 '15 at 21:23
  • @AntonShkurenko unfotunately no, i've just implemented producer-consumer using rx for multithreading. However i was thinking of using PublishSubject for the queue and using map operator to emit new observables. – MightySeal Dec 15 '15 at 04:55

1 Answers1

8

Just use a scheduler based on a single thread executor:

Scheduler scheduler = Schedulers.from(Executors.newSingleThreadExecutor());
observable.observeOn(scheduler). ...
Dave Moten
  • 11,957
  • 2
  • 40
  • 47