• 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

Java Objects in Multiple Threads

 
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

* In a Java based web application, if we check the value of an instance variable, each Thread (Request to the Server) has different values.
* This means each thread has newly created object of a Java class.
* But when we talk about thread safe Java methods, it's recommended that you use "Synchronized" to protect particular steps in a method for thread safety.

So my question is, if each thread create a brand new object, how come different threads invoke the same method? All instance & local variable will be completely new from thread to thread.

Please help to understand this.

Thanks
Uditha

************************************************************************************************
Reference:
Java class that will be invoked by URL
************************************************************************************************

public class HelloObjectWorld
{
private double testVariable;
private static int testConstant;

public HelloObjectWorld()
{
testVariable=Math.random();
System.out.println("############################ I AM AN INSTANCE VARIABLE" + testVariable); // Different for each Thread(Request)
testConstant=121;
System.out.println("############################ I AM A CLASS VARIABLE" + testConstant); // Same for every Thread
}
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have declared testConstant as static, by default static members are shared by all threads in Java.


JLS
17.4.1 Shared Variables

Memory that can be shared between threads is called shared memory or heap memory.
All instance fields, static fields and array elements are stored in heap memory.


So that is why testConstant is always the same.

In answer to your other query, threads can invoke the same method at more or less the same time. Since the method in your example can't show any interference between threads it works out ok here. But if your example was trying to write to a file from two sources, the threads would be executing at the same time and the order in which you write to the file would be wrong. Ill post an example in a few mins.

Hunter
 
Uditha Perera
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hunter McMillen wrote:You have declared testConstant as static, by default static members are shared by all threads in Java.


JLS
17.4.1 Shared Variables

Memory that can be shared between threads is called shared memory or heap memory.
All instance fields, static fields and array elements are stored in heap memory.




Thanks Hunter.
Ya I knew that all Static members are easily shared. But what I want to know is is the same Object of the class is shared between threads?
If that happen only, two or more methods can invoke same method same time right?
Else different threads are offered different object of the same class. (see different thread has different value for my instance variable)
You got my point?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Uditha Perera wrote: . . . Static members are easily shared. . . .

It is not a case of "easily shared". There is only one copy of each static member. If that member is passed to a thread, then it is passed. If it is passed to two threads, then it is passed to two threads. It is automatically and implicitly shared, not "easily". It is a bit like being out in the desert and having one water bottle. You might find it difficult to share the water bottle, but if anybody drinks from a water bottle, then that one bottle is shared.

Remember: one static member per name only. If it is used in two places, it is shared automatically. You can only have two different static members if you give them different names.I hope those three examples of bad design show you what I mean
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Uditha Perera
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell ,
Thanks for Welcome me.

But see. you had given detailed explanation based on "static". which is special case regarding modifiers.

But my original question was, please understand,
I am getting different number for instance variable in different threads. But I have re initiated it in constructor.

So the outcome = Different thread has called the constructor again & again = Different thread has different objects = two or more threads CANNOT invoke
the same method same time.

So what is a method's "THREAD SAFETY"?
Are instance variable shared? Without objects in memory shared ???
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you call the constructor in different threads you will have different instances. That is a special case; it is not usual for each thread to create its own objects. You can manipulate the same object in several threads, in which case you may obtain conflicting and inconsistent results if the objects are not thread-safe.

Any field can be accessed by several threads, maybe simultaneously. Objects are "shared" if you share them, and not shared if you don't share them.
 
Uditha Perera
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: Objects are "shared" if you share them...


Do you mean something like Singleton class?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can share a singleton if you so wish, but that wasn't what I meant. I meant that the programmer shares objects. If you tell two threads to use the same objet, then you are sharing the one object between the two threads. In this case "share" has the same meaning it has in common English (look for "verb").
 
Uditha Perera
Greenhorn
Posts: 8
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:You can share a singleton if you so wish, but that wasn't what I meant. I meant that the programmer shares objects. If you tell two threads to use the same objet, then you are sharing the one object between the two threads. In this case "share" has the same meaning it has in common English (look for "verb").



Ok. It's very true if you write code like,

---------------------------------------------------------------
MyClass myClass=new MyClass();

MyThread t1=new MyThread(myClass);
MyThread t2=new MyThread(myClass);
---------------------------------------------------------------

But how it happens in web application? I mean 2 or more requests == 2 more more Threads right?
How hose threads share same object among them in web application?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If they share the same object, it is because somewhere there is something like what you wrote above:
Thread t1 = new Thread(obj);
Thread t2 = new Thread(obj);
 
reply
    Bookmark Topic Watch Topic
  • New Topic