Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

variables in Threads  RSS feed

 
sanj singh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all
If i declare a class with two private variables and their are twenty threads running then will each thread have its own copy of the two variables or will they share the variables?
Regards
sanj
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If they share the objects, they will share the variables.
- Peter
 
sanj singh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter
Thanks a lot for your prompt reply.let me take a typical scenario.If we create a bean for a JSP page with the two private variables then do we have to synchronize the getter and setter methods since the same bean might be used by multiple users.

regards
sanj
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on whether the bean object is shared by multiple threads - in a JSP, that means it depends on how the bean is scoped.

  • Page scope: there is a bean instance for each page invocation, there is only a single thread accessing a given bean instance.
  • Request scope: see page scope.
  • Session scope: there is a bean instance for each user. Most developers assume that this means there is only a single thread accessing it, but this is not necessarily true! A browser can easily send multiple concurrent request when loading a frameset or when the user gets impatient and refreshes the page. The most conservative assumption is that session-scoped beans need to be threadsafe.
  • Application scope: the same bean is shared by all users, its member variables are accessed by multiple threads. The bean definitely needs to be threadsafe.

  • Beware that the above applies to instance variables only. Class (static) variables are always shared by all threads using an object of that class.
    - Peter
 
sanj singh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter
Thanks for your excellent reply.I have understood page,request and application scope but not understood session scope very well.In the session scope you have mentioned that there is a bean instance for each user.So if the same user refreshes the page then why do we need to synchronize the private variables----even if the user refreshes the page and creates a new thread don't you think that the variables will be unique to his thread.I hope I am able to make my point across.
regards
sanj
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sanj singh:
So if the same user refreshes the page then why do we need to synchronize the private variables----even if the user refreshes the page and creates a new thread don't you think that the variables will be unique to his thread.

If the user refreshes the page before the previous request has finished, there will be two outstanding requests, each with its own separate thread. They can access the same (session-scoped) object. Even though these two threads belong to the same user, it's still multi-threaded access and your object should be threadsafe.
Having said that, I've seen lots of web applications that ignore this without apparent ill effect. Still something to be aware of.
- Peter
 
sanj singh
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Peter
Your replies have been excellent.This is just out of curiosity---can you tell us where do you work?
regards
sanj
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're most welcome, Sanj. For more information about my humble self and the assorted bartenders and sheriffs on the 'ranch, you can take a look at the contact page.
- Peter
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!