I runned your code on my app, the changes I made:
mustAlwaysBeZeroOrOne is static, because I'm using Stateless bean (just
to be sure)
try catch around thread.sleep
Deploy, and open 3 tab which loads the exact same page and in the log is
always 1, and the pages in some kind of FIFO they were served the same
order, like I called them and the whole thing lasted 15s.
So yes, it is thread safe. ;)
Peter
2009-12-01 20:22 keltezéssel, glassfish_at_javadesktop.org írta:
> The INSTANCE trick defeats the thread-safeness of JEE.
>
> I think you will find that if you implement business function like this:
>
> private int mustAlwaysBeZeroOrOne = 0;
> public void myBusinessMethod() {
> mustAlwaysBeZeroOrOne++;
> System.out.println("The variable is: " + mustAlwaysBeZeroOrOne);
> Thread.sleep(5000L);
> mustAlwaysBeZeroOrOne--;
> }
>
> You will find that if you use your bean, through getInstance() trick, you will find that mustAlwaysBeZeroOrOne will increment up to the number of simultaneously running client threads.
>
> I.e. not thread safe.
>
> Thanks
> Jacob