Nic Ferrier wrote:
> I see your problem.
>
> Your problem is that you haven't tried to understand how threading
> works in Java.
>
> My advice is go and read a book that talks about threading. Or go and
> read the archives of this list which explain the situation.
>
> As a gesture of goodwill here's an example servlet which does what
> you want:
>
> public class Counter extends HttpServlet
> {
> Integer lock=new Integer(0);
> int counter=0;
>
> public void doGet(....
> {
> synchronized(lock)
> {
> counter++;
> }
> .
> .
> .
> }
> }
>
Hi Nic , thanks for your email ! but from several emails
in Servlet-List, I guess perhaps there are some potential
security-leak in this way, but now I don't have a testing to
prove it or deny it -- the following is just my guessing:
0
because now *counter* and *lock* are instance field, so if there
are a instance pool which includes several instances
of the class Counter, now diferrent thread will be made
by diferrent instance, and so they will use diferrent
*lock* to lock the code, and so now perhaps there
are more than one threads will run the same code
co-currently. and now I guess the sum of all counter
is the accessing time we want to get.
1
and several weeks ago I think if I use static class field,
I can solve this problem. But form several emails, for
the same Servlet class, perhaps it will be loaded by
diferrent ClassLoader, so now I guess we will have
more than one static class field. For example, in this
case, if we use *static int counter* and/or *static Integer
lock* , we will have several *lock* and *counter* , and
perhaps the sum of all of the counter is the accessing
time we want to get.
2
so sevaral days ago I hope the Sevlet engine provider
will give us a solution , and I guess the interface
SingleThreadModel perhaps is a standard way. :-)
Thanks again :-) I am glad that you are interesting in it ,
and I hope you and other Servlet experts in this List can
spend some time to explain it clearly, I am just very
interested in it, but I don't have a testing -- because
I find I only can make a testing enviroment which is
single ClassLoder -> single instance -> multithread :-)
in this case:
* I can lock my code by both:
[a] static class field
[b] instance field
* I also can lock my code by both:
[a] static synchronized method (which uses the only one class which is
loaded by the ClassLoader)
[b] synchronized method (which use the only one instance)
But I think such testing is not enough, because I guess perhaps
other Servlet engines will use instance pool or something else to
improve the performance. so the day before yesterday I suggest
to make a testing together :-)
Bo
Nov.03, 2000
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html