调用java静态方法与对象创建的性能

调用java静态方法与对象创建的性能

本文介绍了Worklight:调用java静态方法与对象创建的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建议我将自定义java方法作为静态OR从适配器通过java对象访问吗?

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?

或者如果我通过首先声明该类对象然后访问来访问所有自定义java方法方法,提供参数....所以这样当多个用户同时访问相同的方法然后他们将获得正确/相关的数据?

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.

谢谢
Abdul Ahad

thanksAbdul 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)
        };
    }


推荐答案

我相信你很困惑静态字段的静态方法。静态方法只是与对象的任何特定实例无关的代码 - 基本上任何不使用超级引用可以是静态的候选者,前提是它们不会覆盖另一个方法而不打算被覆盖。静态方法没有任何额外的顾虑w.r.t.与普通方法相比,多线程。

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...

这篇关于Worklight:调用java静态方法与对象创建的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 07:32