aspose file tools
The moose likes Threads and Synchronization and the fly likes variables in Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "variables in Threads" Watch "variables in Threads" New topic
Author

variables in Threads

sanj singh
Ranch Hand

Joined: Jun 30, 2001
Posts: 129
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

Joined: Apr 20, 2000
Posts: 3252
If they share the objects, they will share the variables.
- Peter
sanj singh
Ranch Hand

Joined: Jun 30, 2001
Posts: 129
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Jun 30, 2001
Posts: 129
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

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Jun 30, 2001
Posts: 129
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

Joined: Apr 20, 2000
Posts: 3252
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: variables in Threads
 
Similar Threads
ThreadSafe
Two threads of same class instance to wait separately
Why can't we access variables from the parent page in Dyanamic Include?
Confused about thread safety
Are Local Variables Thread Safe