2

I am building a webcrawler which is using two classes: a downloader class and an analyzer class. Due to my design of the program I had some methods which I outsourced to a static class named utils (finding the link suffix, determining if I should download it given some variables, etc.). Since at a certain time there is more than one downloader and more than one analyzer I'm wondering whether they can get a wrong answer from some static method in the utils class.

For example, say the analyzer needs to know the link suffix - it's using the utils.getSuffix(link) method. At that same time the OS switches to some downloader thread which also needs to get some link suffix and again uses utils.getSuffix(link). Now the OS switches back to the analyzer thread which does not get the correct response.

  1. Am I right?
  2. In case I'm right should I add synchronized to every method on the utils class? Or should I just use the relevant methods in every thread to prevent that kind of scenario even though I'm duplicating code?
4
  • It is impossible to answer without having more information: does the method access static variables? Is the parameter (link) mutable and could it be modified by another thread? Commented Jan 23, 2013 at 12:21
  • Please post the code in question. Commented Jan 23, 2013 at 12:21
  • when a method is called, a stack frame is created on the caller's thread which contain arguments and local variables. So thread switching is transparent, you have no means to notice if there are another threads calling the same methods. Commented Jan 23, 2013 at 12:31
  • each method in the utills class uses some final variablesI decalred inside it, with so some of the parameters are shared by all classes (the lists which holds the html pages and links to be downloaded) but those are coming from java.concurrent so i assumed they take care about it by themselves... =\ Commented Jan 23, 2013 at 13:15

1 Answer 1

2

This entirely depends on the implementation of the method. If the method uses only local variables and determines the suffix based on the parameter you give it, all is well. As soon as it needs any resource that is accessible from another thread (local variables and parameters are not) you'll need to worry about synchronization.

It seems to me you're using statics as utilities that don't need anything outside their own parameters; so you should be safe :)

Sign up to request clarification or add additional context in comments.

3 Comments

any modifiable resource. If the resource is static constant array/map/table, it's no harm to access it simultaneosly from different threads.
@AlexeiKaigorodov not quite right - the constant class needs to support concurrent access too, so a non Concurrent/Synchronised collection class would need to be protected by a lock, whereas one that was Concurrent/Synchronised wouldn't need that protection.
The thread which is using the static method (the caller) pass arguments to the static function (the callee) to be proccessed, some of those arguments are global variables (since the webcrawler is designed as a producer consumer pattern when each of the analyzer and downloader functions as both, the broker is the webcrawler itself that gets the intial params to work with and send a reference of them to the threads he create) with so the global arguments are LinkedBlockingQueue from java.util.concurrent (im using them to hold the links to be downloaded and the html pages to be analyzed).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.