• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Visitor Counter Problem...

 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rahul Nair
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vivek,

Thank you very much for your help!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic