karthikeyan ts wrote:Hi Steve,
Thanks for the answer. I've one doubt in the code. Could you explain.
Whether each thread will look the condition. Once the thread is started, then first time only it will look the condition.
In my example, I wrapped the work into a while loop, so the variable would get checked at the start of each while loop - and the contents of the while loop would be one un-interruptible
unit of work needed to check for the word. If you can't put your work into a loop then you would need to periodically check on the variable:
Also could you please if all the 4 threads are running in different JVMs, then how to achieve the same result.
One point i know that if cross JVM Thread program, common flat file can be used. but where we can place the common flat file. How the threads running in different JVMs will read/write the flat file. whether using normal File IO stream?
If you use a flat file:
Q: Where to place the common flat file?
A: Anyplace where all of the JVMs can see it.
Q: How to read/write the file?
A: Using normal File IO, or the NIO packages. Either way
you should probably try to use FileLock to make sure you don't try to access the file in multiple JVMs.
But as Sunny and Chris pointed out there are better ways to do this. One method that I like better than Files but which requires less work than RMI (not sure about terracotta) would be to use a database. That way you can let the DB handle synchronization for you, and it is a lot easier to access a DB safely from multiple JVMs or computers than it is to access file systems.