Rasoul Miri
Rasoul Miri

Reputation: 12222

set name for a thread in RXJava android

I now when creating a Thread in java I can set a name for it.

Thread foo = new Thread("Foo");

Can I set name for Thread in RXJava?

Upvotes: 1

Views: 685

Answers (1)

Andrei Tanana
Andrei Tanana

Reputation: 8422

RxJava doesn't use threads by itself. It uses schedulers. Threads in existent schedulers already have pretty clear names but if you want to name threads in your custom new scheduler you can use a ThreadFactory:

fun main() {
    val pool = Executors.newCachedThreadPool { runnable -> Thread(runnable, "My thread") }
    val scheduler = Schedulers.from(pool)
    Completable.fromRunnable {
        println(Thread.currentThread().name) // will print "My thread"
    }
        .subscribeOn(scheduler)
        .subscribe()
}

Upvotes: 4

Related Questions