Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Understanding SingleThreadModel

 
Ravinder S Edhan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I'm try'n to understand the JSP.

If I have a instance variable in my JSP page. Do I have to implement the synchronization block on that variable or not?

I had also read in one of the reference that instance variable should not be used in JSP. To use declaration in JSP, create non-changing variable. What does non-changing variable means and how can we create it?

Ravinder
 
Daniel Rhoades
Ranch Hand
Posts: 186
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Running an application using the SingleThreadModel is pretty useless for most purposes - all it does is hurt your concurrency, its better to use syncronisation blocks when required. As for instance variables - avoid using them if you can because of threading issues, making the instance variable final and static may work for you, but remember for final object references you are only making the reference final not the object's properties.
 
Ravinder S Edhan
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Daniel,

One more thing I would like to know ... suppose I'm having following code ..

<%! int counter = 0; %>
<%= ++counter %>

Whenever I hit for new request ... the 'counter' get incremented. Shall we assume that all instance variable are 'static'.

Ravinder
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSPs get compiled into Servlet code.
The container will create one instance of each servlet.
That one instance will handle all requests (seperate thread for each).
An instance variable in that servlet will be common to all threads.
That's why your int variable is being incremented.

Static means that all instances of that class will point to the same variable.
That instance variable will only be static if you declare it to be.
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With Tomcat I've encountered cases where a non-static instance counter would be incremented several times (less than 10 I believe), and then start from zero again, implying that Tomcat for some reason decided to create a second instance of the JSP Page's servlet.

-Yuriy
 
Run away! Run away! Here, take this tiny ad with you:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!