4

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)
        };
    }
AAhad
  • 2,805
  • 1
  • 25
  • 42
  • It all depends on what your method does and how it interacts with the object's members (if any). If all you had is a simple `public static double add(double a, double b) { return a+b; }` then of course you can use it in parallel by multiple threads. More information about what you are doing will help – Aviram Segal Mar 03 '13 at 07:49
  • @AviramSegal Yes, its similar like " public static String getMyBalanceMessages(String userAct) { return xmlBalanceStatements; } " Now if multiple users concurrently access it via WL adapter, so will it bring different results ? or it will bring correct results.....as the adapter call is asynchronous.. .thanks – AAhad Mar 03 '13 at 08:09
  • How is `xmlBalanceStatements` defined? Is it a constant? What's the point of the `userAct` argument since you don't use it at all? SHow us real code. The code matters. – JB Nizet Mar 03 '13 at 08:11

1 Answers1

2

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

thkala
  • 84,049
  • 23
  • 157
  • 201
  • Yes i know the difference.But am not sure how WL Adapters internally deal with them.for example, if i access static methods in adapter OR if I first create object & then access methods..will both be internally treated in the same way...called in the same way.. when concurrent users access them? Bcz if creating objects first and then accessing methods then each time one object will be created that's an overhead for GC. but on the other hand if multiple users access static method in WL adapter then Will it give different results or NOT? How Worklight adapters interact with java .Thanks – AAhad Mar 03 '13 at 08:18