I have this crucial problem where I have java stand-alone application. I need to request some parameter from web-service to my java application and after that my application send the same parameter to my smart meter. After sending params to the smart meter, my application needs to wait till the result came from the smart meter.
For the stand-alone application, I use jersey library. The jersey library reserve at-least 10 threads for processing the web services. You see I need to hold the specific the Thread for waiting for the result from the smart meter. For this specific problem, I create a MutexHolderCollection which holds the current thread.
Below is the MutexHolderCollection.
class MutexHolderCollection {
private val mutex = Mutex(true)
private val mutexHolders = mutableMapOf<String, Thread>()
fun blockThread(key: String, blockingThread: Thread) {
if (!mutexHolders.contains(key)) {
mutexHolders[key] = blockingThread
runBlocking {
mutex.withLock(blockingThread) { startTimer(blockingThread) }
}
}
println("Mutex Complete")
}
private fun startTimer(blockingThread: Thread) {
Observable.timer(1, TimeUnit.MINUTES)
.subscribeOn(appRxSchedulers.network)
.subscribe({
blockThread(blockingThread)
}, Throwable::printStackTrace)
}
fun unlockThread(key: String) {
if (mutexHolders.contains(key)) {
val blockingThread = mutexHolders[key]
mutex.unlock(blockingThread)
}
}
private fun blockThread(blockingThread: Thread) = mutex.unlock(blockingThread)
}
Now I’m successfully able to lock the thread but when I’m trying to unlock it gives me this exception.
The Mutex is locked by LOCKED but expected Thread[Grizzly(8),5, main].
Do someone have any idea to solve this problem.
Thank you for your time…