This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Inter Thread communication  RSS feed

 
Karthikeyan Sivanaiah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Team,

If 4 threads are involved simultaneously to search a word in different servers. if one thread is found that word, the other 3 threads should be notified and stopped.

Here wait/notify mechanism cant be used. How can we achieve this?

Thanks in advance.
 
Mohamed Sanaulla
Bartender
Posts: 3169
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you can use one of the IPC methods. Using Sockets to send and receive data?
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are the Threads running on the same JVM and searching different servers, or are they on different servers/JVMs?
 
Karthikeyan Sivanaiah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve,

All the threads are running same JVM to search different servers.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then the answer is much simpler - provide a variable (volatile or protected by synchronized blocks) which indicates the word was found. Then each Thread checks back on the variable periodically... something like this:
 
Karthikeyan Sivanaiah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

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?
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

when you have 4 JVM/Machines, one way could be - RMI.
Give call to a class and create a flat file.





Difference between Waiting and Yielding in Threads
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or for 4 JVMS use terracotta ,the advantage being you could stick to the same wait/notify code and just configure terracota appropriately.
 
Greg Charles
Sheriff
Posts: 3014
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
karthikeyan ts wrote:
Whether each thread will look the condition. Once the thread is started, then first time only it will look the condition.


I think Steve's pseudocode simplifies, because we don't really know what your search algorithm looks like. What you want is for the search process to periodically check the flag value, and just return if some other thread has found the result already. So if you were doing a tree traversal, for example, you might check the flag before you go to the next node, not just when you first dive into the tree. Presumably any search you do will involve some kind of loop, so check the flag at the beginning of the loop.

For the multiple JVM case, I'd say take advantage of the Oberserver/Observable pattern. Java already has interfaces to get you started, and every beginning Java networking book I've ever seen runs through how to implement an RMI version.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Karthikeyan Sivanaiah
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Steve,

Yes i got some points from you and Greg Charles, Sunny Jain.
I'll try your suggestions.

I thank you all for the great help.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!