Worklight: Performance of calling java static method vs object creation

半世苍凉 提交于 2019-12-23 12:57:12

问题


I need suggestions either I make custom java method as static OR accessing via java object from an Adapter?

My scenario is: thousands of users are making transactions and each user is accessing the same method again & again and just changing some values specific to that user or transaction.

Now if I am making them as static methods then will it cause problems for users, as we know the adapter call is asynchronous....so if multiple users calling same method at the same time then will it cause problem is returning different values to each other?

Or if i access all custom java methods via first declaring that class object and then accessing methods, providing parameters....so in this way when multiple users access the same method at the same time then they will get proper/relevant data?

From performance point of view which approach is good and does static method approach bring wrong data to users.....one user's data to another, and others to another person.

thanks Abdul Ahad

------------ my code is like---

java code:

  public static String getBalanceSummaries(String userAct){
            String replyMsg="";
    try {

    replyMsg = getBalanceStatementfromMQ(userAct);

    }catch(Exception e) {}

    return replyMsg;

    }

  -----WL Adapter code:------

    function showAllBalace(userActNo){
        return{
            result: com.my.package.getBalanceSummaries(userActNo)
        };
    }

回答1:


I believe that you are confusing static methods with static fields. Static methods are just code that is not associated with any specific instance of an object - basically any method that is not using this or super references could be a candidate for being static, provided that they are not overriding another method and are not intended to be overridden. Static methods do not have any additional concerns w.r.t. multithreading when compared to "normal" methods.

Static fields, on the other hand, are by definition shared among all threads and access to them should be protected as with any shared resource. Any method using a static field, regardless of whether the method itself is static or not, should be inspected for concurrency issues.

As far as performance goes, there is anecdotal evidence that static methods may provide performance improvements when compared with normal virtual methods, but quite honestly I would not worry about it until a profiler tells me to. Premature optimization is the root of all evil...



来源:https://stackoverflow.com/questions/15183295/worklight-performance-of-calling-java-static-method-vs-object-creation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!