Cham Wick wrote:Thanks. But can you please write a piece of code on how to do that.
Yes, I can.
I won't though. I told you was has to be done. Now it's up to you so ShowSomeEffort and try to do it. Employ some logic and educated guessing, try a couple things, and if you get stuck, show what you tried and explain what difficulty you're still having.
Why do you have a static member variable TRead tr in line 6? You have a method getInst() (lines 21-23) which I would have expected to be static too (since all it does is return the static member variable). That method is not called anywhere (well, it is in line 27, but that's commented out).
Did you understand what Jeff said? Synchronization in Java happens on objects. Each Java object has a lock associated with it. When you synchronize on an object, the thread that wants to execute the synchronized block or method will try to acquire the lock for the object. Only one thread can hold the lock of an object, so if another thread already holds the lock, the thread that tries to execute the block is forced to wait until the other thread releases the lock.
In your code as it is now, you are starting three threads, but you have three different TRead objects. The three threads will lock those three different objects, which they can do without needing to wait; only when threads try to lock the same object, there's a chance they have to wait for each other.
You'll have to make some fundamental changes to your code to see it work. One thing is that you should not make TRead extend Thread, or make the code synchronize on something else than a TRead object.