• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inter Thread communication

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you can use one of the IPC methods. Using Sockets to send and receive data?
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Steve,

All the threads are running same JVM to search different servers.
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 443
3
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

reply
    Bookmark Topic Watch Topic
  • New Topic