Reputation: 2330
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.
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? Upvotes: 2
Views: 1025
Reputation: 9231
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 :)
Upvotes: 2