I have Runnable queue that invokes one by one in specified thread.
val queue = LinkedBlockingQueue<() -> Unit>()
val queueThread = thread {
while(true)
queue.take().invoke()
}
Also I have function that adds Runnable and wait for it to complete.
fun invokeOnQueueThread(toInvoke: () -> Unit){
if(Thread.currentThread() == queueThread)
toInvoke()
else {
queue.offer(toInvoke)
// Some waiting code...
}
}
I test if current thread is already inside my queue's thread to prevent unnecessary locking itself.
So, the question is:
Will Thread.currentThread() cause performance issues if I call this code very, very often?