We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,657 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Is there any problem if a static method is accessed by mutiple threads at once

I have a kind of doubt, suppose I have a class like this:

public class AA{

    public static createUser(String a, int b){
        User usr=new User(a,b);
        BB.storeUser(usr);
    }
}

Now, if multiple threads call this createUser() method of this class at the same time, will it cause any kind of problem. Do I need to make it synchronous? It is not going to do any sophisticated task, it will just create a User object and call a function to store it.

3
Contributors
5
Replies
22 Hours
Discussion Span
1 Year Ago
Last Updated
6
Views
Question
Answered
warlord902
Junior Poster
121 posts since Dec 2010
Reputation Points: 19
Solved Threads: 0
Skill Endorsements: 0

This should be fine, but you probably want the BB storeUser() method to apply a lock to the data structure where it is placing the usr object.

rubberman
Posting Maven
2,571 posts since Mar 2010
Reputation Points: 365
Solved Threads: 305
Skill Endorsements: 51

This should be fine, but you probably want the BB storeUser() method to apply a lock to the data structure where it is placing the usr object.

Means unless we are updating or modifying something there is no need to make such methods synchronous?

warlord902
Junior Poster
121 posts since Dec 2010
Reputation Points: 19
Solved Threads: 0
Skill Endorsements: 0

Means unless we are updating or modifying something there is no need to make such methods synchronous?

This will be true most of the time, but if you are accessing more than one value in such a method you may get inconsistent values for them when another thread modifies the data between the accesses. I agree with rubberman, the storeUser method should be synchronized on the target data store object.

JamesCherrill
... trying to help
Moderator
8,502 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,454
Skill Endorsements: 29

Means unless we are updating or modifying something there is no need to make such methods synchronous?

As long as you are only using local, and not static or global variables in your method, you should be fine. As we have stated however, the storeUser() method should either be synchronous, or a lock applied to the data store. If the storeUser() method does a lot and being synchronous could introduce unnecessary latencies, then use a lock only for access to the data store.

rubberman
Posting Maven
2,571 posts since Mar 2010
Reputation Points: 365
Solved Threads: 305
Skill Endorsements: 51

Thanks a lot for the replies, :)
cleared my doubt

warlord902
Junior Poster
121 posts since Dec 2010
Reputation Points: 19
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by rubberman and JamesCherrill

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0763 seconds using 2.7MB