0

Probably the stupidest question you have ever heard.

Inside the Web container how more than 1 object of the same class is getting created/managed which has same reference variable... Let me explain with an example.

Inside my controller class I have a code piece

AdminUser adminUser= new AdminUser();

So when 2 Admins signs-in to my web application, there will be 2 Objects of the class AdminUser with same reference variable "adminUser"

  1. How is it possible, is it 2 different threads?
  2. Who is managing this threads, web container?
  3. If so, how web container is doing it, is it wrapping application code with threadLocal?
  4. If its different threads, to maintain a global object (say a counter for the admins visit counts), "static" won't suffice... it needs to be "volatile" instead, correct?
KingJulien
  • 303
  • 3
  • 9

3 Answers3

1

So when 2 Admins signs-in to my web application, there will be 2 Objects of the class AdminUser with same reference variable "adminUser"

No.

If that line of code is in a method, the variable is on the stack, and there can be as many instances as there are concurrent invocations of the method, including recursions and calls by multiple threads.

If it's non-static member initialization code, the variable is in the object, and there are as many instances as there are objects.

If the object is a bean, the number of them depends on the object's scope: if application, one; if session, one per session; if view, one per view; etc.

If it's static member initialization code, it shouldn't be.

How is it possible, is it 2 different threads?

See above.

Who is managing this threads, web container?

Yes, and it is also managing bean instances.

If so, how web container is doing it, is it wrapping application code with threadLocal?

No. See above.

If its different threads, to maintain a global object (say a counter for the admins visit counts), "static" won't suffice... it needs to be "volatile" instead, correct?

No. You can maintain it as an instance member of an application-scoped bean.

You should avoid statics completely in a web-app, apart from constants and caches, which you should also avoid.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks EJP for the Answers... Its still not clear for me, bear with me for a couple of questions more.. When I said "2 Objects of the class AdminUser" I meant 2 session scoped bean only. So in my JVM, 2 objects/beans of type AdminUser which has reference variable adminUser co exist... right? how? is this 2 objects are created by 2 different threads.? – KingJulien Jul 11 '14 at 05:33
  • If you have two session scoped beans, you have two sessions. Irf you have two AdminUser session-scoped beans, the admin usr has logged in twice via two separate sessions. – user207421 Jun 16 '15 at 22:43
0

Are several factors that influence to answer your questions, the j2ee has own specification that manufacturers must follow, and others, they should simply implement as they wish. Is to give the performance they want, or how they should act in certain moments. A Jboss is certainly different from Apache Tomcat performance issues, which makes it hard to answer your first question. Then answer: "It depends on which vendor you are talking. Specifically what Container?" It also depends if you implement Enterprise JavaBeans in your code because then probably several Beans (objects) will be started (depending on the container) and each request from a client he might share these Beans or create new ones to account for the competition.

Answering their fourth question, the best method would be a counter through a Singleton Session Bean (EJB implementation), in my opinion. But you could do through a static class, in which all other objects can share, but would have to resolve the competition between objects. EJB resolves this competition for you without you having to waste time with it. is for this and many other things the solution of the EJB

I think if you do not implement EJB, and make a very simple code, for each request that a customer does, an object will be created and destroyed (not really, because its depends the JVM to destroy, garbage colletction, but its inaccessible to you) after sending the response to the client container. But again, depends on how you program, of which container ... I hope I have helped you in your inquiry.

I suggest reading the J2EE specification: JSR-000342 EE 7 Specification And a good book to read about EJB's: Enterprise JavaBeans 3.1, 6th Edition By Andrew Lee Rubinger, Bill Burke

0

This was the answer that I was looking for. Thanks everyone who answered.

Does application server create new thread for each request from same user?

Web Container (Tomcat) spawns new thread for each request (not really, it uses thread pool for performance reasons).

For any request (doesn't matter who made it) a thread is obtained from the pool, the request is processed then the thread goes back to the pool.

Community
  • 1
  • 1
KingJulien
  • 303
  • 3
  • 9
  • This doesn't answer your question. Whether it's a new thread or a pooled thread is completely irrelevant to the question of how two session beans got created. – user207421 Jun 16 '15 at 22:48