aspose file tools*
The moose likes Struts and the fly likes Visitor Counter Problem... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Visitor Counter Problem..." Watch "Visitor Counter Problem..." New topic
Author

Visitor Counter Problem...

Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138
hi,

I am developing a web application using Struts and I want to show the website visitor counter to user.
I have implemented it using Cookie. When new user visit the cookie is added and the counter in the database is incremented.
As there simultaneous access to counter during visits...and to prevent from reset to counter to some wrong value during
multiple access, I have use the synchronized block for counter increment operation.

But because of synchronized...the website takes very long time to load.

I am quite confued now, so can you please help me in this regard ?
what should i use instead of synchronized block...or any other solution which solves the above problem ?

Thanks

Rahul


Vivek Kr Singh
Ranch Hand

Joined: Oct 12, 2007
Posts: 56
Use java.lang.AtomicInteger or java.lang.AtomicLong. Invoke incrementAndGet() to get new value for counter. No explicit synchronization needed. Don't store response value in a reference.


SCJP 1.4
Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138
Vivek Kr Singh wrote:Use java.lang.AtomicInteger or java.lang.AtomicLong. Invoke incrementAndGet() to get new value for counter. No explicit synchronization needed. Don't store response value in a reference.


"store Resonse Value ina reference" i did not understand this statement ?

So can you please tell me in detail ?

Thanks

rahul
Vivek Kr Singh
Ranch Hand

Joined: Oct 12, 2007
Posts: 56
Piece of code below is thread safe, so no need for synchronized block



Piece of code below is not thread safe as value can be modified by another thread when not using synchronized block

Rahul Nair
Ranch Hand

Joined: Dec 01, 2007
Posts: 138
Hi Vivek,

Thank you very much for your help!
 
 
subject: Visitor Counter Problem...