• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

static method in multiple-threaded environment

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 76394
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I am mighty! And this is a mighty small ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic