Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

synchronized  RSS feed

 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody,
Acually I have a problem with threads .Im using a jsp page and a part of my code uses static variables.So this jsp page is used by many and its locking the page.So I used a synchronized block in this context.
synchronized(this){static variables shared by many users }.Is this solution ok.Or do I need to used synchronized variables.Please let me know
bye
nitin
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using static synchronized objects in a JSP is probably not a good idea if you can avoid it -- multiple clients of your JSP will block waiting for each other.
If you have to do it, though, synchronizing on "this" may not work; multiple instances of the compiled JSP object could each access the variables simultaneously. Use a static variable as a lock -- even just a special one like
private static Object LOCK = new Object();
invented just for this purpose.
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
Thanks for your reply
So do u want to make all the objects as private.
bye
nitin
 
nitin kumar
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://mindprod.com/jgloss/synchronized.html
hi guys
In the following url he says acess to shared variables can be done by the code
synchronized(getClass()){ }Does this make me thread safe
bye
nitin
 
Pierre Henry
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to prevent simult access to a static var,
the following you can use:

It is not correct to use the getClass() method, cause it would not work on extended classes: such extended method would have a different lock object.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!