• Post Reply Bookmark Topic Watch Topic
  • New Topic

How Threads can share the messages/data.  RSS feed

 
Vignesh Ganesh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have a question regarding threads.

Could any one explain how the following thing can be achieved?

I have 10 large .txt files(each of 500MB lets say)
And there is string which I have to find from these 10 files(Provided the string occurred in only one file and only once).
Now my motive is to create 10 threads and leave each thread on each file for searching the given string.
When a thread among those 10 finds the string in a file that it is searching, then it notifies all the threads that "Hey Wait I found the String. You all can stop now".

How can I achieve this ?
Kindly throw some light on this? Please provide the source code. :P

Best Regards
Krishna
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The only threads we discuss on this forum are discussion threads I shall move you somewhere more appropriate.
 
Campbell Ritchie
Sheriff
Posts: 55351
157
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vignesh Ganesh wrote: . . . How can I achieve this ?
Kindly throw some light on this?
With a shared boolean value.
Please provide the source code. . . .
No. Though you might find somebody who is willing to sell you some code
 
Vignesh Ganesh
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for your reply.
Ok I understand.
But how can a message be shared to all the other threads(to stop) when needsSearch becomes false.


Best Regards
Vignesh
 
Jim Venolia
Ranch Hand
Posts: 312
2
Chrome Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Off the top of my head I can think of 2 ways I've done this, albeit neither was Java.

First, as mentioned above, have a boolean all your threads monitor. When the string is found set the flag to true, all your other threads will notice soon enough.

Second, have the thread that finds the string throw an exception. This exception is caught by the other threads and causes them to exit.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16007
87
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be an interesting opportunity to learn about the Fork/Join Framework, a framework in Java for solving problems in a divide and conquer way (you split up a big task in smaller tasks, solve the smaller tasks, and then combine the results of the smaller tasks to the end result).

If you want to do it in a really advanced way, distributed over multiple computers, you could so research into MapReduce, which is the same idea as fork/join but then in a cluster of computers. There are several Java frameworks for doing this, such as Apache Hadoop or Apache Spark.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!