File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes static method in multiple-threaded environment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static method in multiple-threaded environment" Watch "static method in multiple-threaded environment" New topic
Author

static method in multiple-threaded environment

Andy Yu
Greenhorn

Joined: Jun 12, 2012
Posts: 9
Hi all,
I'm new to Java and I'm designing a static method that can be invoked by many clients, so it is at multiple-threaded application.
what i wonder here is how's the performance of my design ,and whether it is 'safe' in multiple-threaded environment.

and another question, when you are going to design a method providing common-sharing service, how will you do it? I'm just get some 'best practice'

many thanks for your help.

Andy
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
We cannot tell about performance. You should assume your method is not thread‑safe, until you have checked it. Maybe you should give some more details of the method.
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Hi,

Are you trying to use any Thread-safe code? Otherwise, had it been a performance issue to use static methods, then it would not have been used in web applications at all.
But of course use of static should be based on requirements only and should be kept to a minimum

Vishal


Programming is about thinking, NOT coding
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7800
    
  21

Andy Yu wrote:I'm new to Java and I'm designing a static method that can be invoked by many clients, so it is at multiple-threaded application.

Like Vishal, my wonder would be whether the method actually needs to be static. It's a common beginner's mistake, but without more information about what you're trying to do (or, as Campbell said, some actual code) it's difficult to know.

BTW, before you post code, please read the UseCodeTags (←click) page thoroughly.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Andy Yu
Greenhorn

Joined: Jun 12, 2012
Posts: 9
Campbell Ritchie wrote:We cannot tell about performance. You should assume your method is not thread‑safe, until you have checked it. Maybe you should give some more details of the method.


Hi Campbell,
Let's assume there has such method below:
public static String doReverse(String inputString){

// reverse the input string

}

what my problem is whether this method is 'safe' in multiple-threaded application .
as maybe the body of the method needs a little bit times to work out the result,or maybe there has some 'class variable' are referenced based in various reasons.

BTW,designing such kind of method is just want it to be a 'tool' that i can use it in many parts of my application.

Appreciate your help.

Andy
Vishal Shaw
Ranch Hand

Joined: Aug 09, 2012
Posts: 179
Andy Yu wrote:
Campbell Ritchie wrote:We cannot tell about performance. You should assume your method is not thread‑safe, until you have checked it. Maybe you should give some more details of the method.


Hi Campbell,
Let's assume there has such method below:
public static String doReverse(String inputString){

// reverse the input string

}

what my problem is whether this method is 'safe' in multiple-threaded application .
as maybe the body of the method needs a little bit times to work out the result,or maybe there has some 'class variable' are referenced based in various reasons.

BTW,designing such kind of method is just want it to be a 'tool' that i can use it in many parts of my application.

Appreciate your help.

Andy


By 'safe' did you mean thread safe ?

If it is for thread safe, then it's ok as String objects are immutable. But if you have a lot of methods like this which are static, then your memory load will be high. I would like you to figure it out why?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

Thread-safety doesn't have anything to do with whether the method is static or not. You'd need to look at the code in the method to determine whether it's thread-safe.

Thread-safety has to do with whether different threads running the method at the same time could interfere with each other, because the threads would be trying to modify the same data.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7800
    
  21

Jesper de Jong wrote:Thread-safety doesn't have anything to do with whether the method is static or not. You'd need to look at the code in the method to determine whether it's thread-safe.

@Andy: What Jesper says is absolutely right; however, it's worth noting that, all other things being equal, a static method is more likely to experience contention than an instance one. For that reason, you might want to consider something like a ReentrantLock rather than simple synchronization, since they are designed to scale better.

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Vishal Shaw wrote: . . . By 'safe' did you mean thread safe ?

If it is for thread safe, then it's ok as String objects are immutable. . . .
How do you know that method is thread‑safe?How’s that for a bit of bad programming By creating an unnecessary field c, I have made that method very definitely thread‑unsafe.
As I said before, assume your method is not thread‑safe, until proven otherwise.
Andy Yu
Greenhorn

Joined: Jun 12, 2012
Posts: 9
Jesper de Jong wrote:Thread-safety doesn't have anything to do with whether the method is static or not. You'd need to look at the code in the method to determine whether it's thread-safe.

Thread-safety has to do with whether different threads running the method at the same time could interfere with each other, because the threads would be trying to modify the same data.


Hi Jasper,
thanks for your hint, now i understood that whether the method is static or not is not my concern, but what i really need to care about is the codes including static variable in my method body
as the static variable is shared among the threads.
Appreciate it!

Andy
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14150
    
  18

It also doesn't really have to do with variables being static - it is still possible to have multiple threads trying to change the same object, without having any static variables.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static method in multiple-threaded environment