0

I am having servlet in user based web-application on tomcat java. I have a servlet Action class says A. I have also created a custom class says B which have some methods.

My question is : Should i create Staitc method in my custom class or can i call them by instantiating the class . I have explained both scenerio below:

Scenerio First:

Servlet A{

B.methodB() // static method.
}


Scenerio Second:

Servlet A{

B b = new B();
b.methodB() // instance method.
}

Will the scenario first be thread safe in my case ? Does static method always need to be synchronized in user based application ?

gSingh
  • 57
  • 1
  • 14

1 Answers1

0

A web application is always written for users, so it is always user-based. (Or I missed something? )

Scenario First: methodB should be synchronized, but it will create a performance problem as only one thread can access it at a time.

Scenario Second: it will work, but maybe creating an object pool will be better, as objects will not be required to be garbage collected.

Yuriy N.
  • 4,936
  • 2
  • 38
  • 31