简体   繁体   English

Worklight:调用Java静态方法与对象创建的性能

[英]Worklight: Performance of calling java static method vs object creation

I need suggestions either I make custom java method as static OR accessing via java object from an Adapter? 我需要建议还是将自定义java方法设置为静态方法,还是通过Adapter通过java对象进行访问?

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? 或者,如果我通过首先声明该类对象然后访问方法,提供参数来访问所有自定义Java方法,那么以这种方式,当多个用户同时访问同一方法时,他们将获得正确/相关的数据?

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 感谢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)
        };
    }

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. 静态方法只是与对象的任何特定实例都没有关联的代码-基本上,任何不使用thissuper引用的方法都可以成为静态方法的候选方法,前提是它们不会覆盖另一种方法且不希望成为静态方法。覆盖。 Static methods do not have any additional concerns wrt 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... 过早的优化是万恶之源...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM