“I have a XYZ class in that I have written two methods A and B, Now two threads are operating on one the objects of this class, One thread is in the A method, I want that as soon as second thread calls my B method, thread in method A should immediately stop and come out. Would anybody tell me the solution for this?”
Share
Let me answer your question literally and show you why its a bad idea.
This code is flawed in many ways, but if two threads enter this method the first thread could be stopped. However it could leave account1 missing money which was not transferred to account2. (or worse)
In short, it makes no sense to have one thread kill another thread, in fact you so to great lengths to AVOID this, so encouraging it in any way is bad idea.