• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Visitor Counter Problem...

 
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vivek,

Thank you very much for your help!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic