Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static method in multiple-threaded environment

 
Andy Yu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Andy Yu
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Winston Gutkowski
Bartender
Pie
Posts: 10492
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15436
41
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic