wood burning stoves*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Local Vs. Global - Results Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Local Vs. Global - Results" Watch "Local Vs. Global - Results" New topic
Author

Local Vs. Global - Results

Paul McKenna
Ugly Redneck
Ranch Hand

Joined: Jul 08, 2000
Posts: 1006
Hi..
I wrote the following servlet and tested it
public class MyServlet extends HttpServlet {
....
public int j = 0;
public void doGet(HttpServletRequest req, HttpServletResponse res) {
int k = 0;
System.out.println("J : "+j);
System.out.println("K : "+k);
i++;
k++;
}
}
As expected, K remained constant at 0 and J kept increasing.
Then I made the servlet implement Single Thread Model, hoping to see both remain as 0. However this did not happen. Any explanations why? As far as I can see.. since a new servlet instance is created, I should also remain as 0.
Thanks in advance.
SR


Commentary From the Sidelines of history
Nancy King
Greenhorn

Joined: Aug 05, 2002
Posts: 25
Try to use private int j = 0; instead of public int j = 0;

Nancy


Sun Certified Programmer for Java 2 Platform
Yu Chen
Greenhorn

Joined: Dec 03, 2001
Posts: 28
Not sure what is the purpose for this testing. But changing it to private should not make any difference, it only changes the access scope, since the incremens are within a instance method, it won't make any difference.
The container can either do sequence processing of the requests or has a pool instance of this servlet, but in either case, the values of j & k should be changing.
Another point is that the container is going to create multiple instances of this servlet, it would make more sense to make j a static variable than a instance variable.


SCJP2<br />SCWCD
Maha Annadurai
Ranch Hand

Joined: Oct 27, 2002
Posts: 87
This statement "but in either case, the values of j & k should be changing." in above reply is not correct.
Only J will be incremented for subsquent requests. K will not be incremented , since it is a local var and each request gets a fresh initialized copy each time it enters the service(req,res) -->doGet(req,res) methods.
Regards,
Maha Anna
k space
Ranch Hand

Joined: Jun 25, 2002
Posts: 104
Then I made the servlet implement Single Thread Model, hoping to see both remain as 0. However this did not happen. Any explanations why? As far as I can see.. since a new servlet instance is created, I should also remain as 0.

Implementing SingleThreadModel does not necessary to create a new instance. It only guarantees that no two threads will execute concurrently in the servlet's service method.
The servlet container may synchronize accesses to a single instance, or maintain a pool of instances.


SCEA | SCBCD | SCWCD | SCJP - The kSpace
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Local Vs. Global - Results