datacorruption and static methods
public static void myMethod( String Argument)
{
int i = 0 ;
.....
}
What are the chances of data corruption in this static method?
IF there are chances of data corruption, am I corrupt in saying that chances of data corruption for argument and Variable i are same?
Where is function state stored in static method stored? What will happen to this if one thread pre-empts other?
public class a
{
int j;
public static void myMethod( String Argument)
{
int i = 0 ;
.....
}
}
How is integer j different from integer i? Where will JVM keep these variables ?
The static method myMethod as you have it there is thread safe. The variable i is not shared between two calls to this method.
Your variable j is not static, so it cannot be used in your static method myMethod unless you instantiate your class. However if you make it static and you modify it in myMethod, then your method is no longer thread safe since two threads can call myMethod at the same time and update j.
You can make your method static by using the synchronized keyword.
> What are the chances of data corruption in this static
> method?
None. As long as this static method only acts on local variables (variables declared inside the method) each call to the method is completely separate from any other call.
> Where is function state stored in static method
> stored? What will happen to this if one thread
> pre-empts other?
I'm not sure what you mean by 'function state' but static methods are not associated with an Object, though they can modify and depend on the state of static (class) varaivbles.
> How is integer j different from integer i? Where will
> JVM keep these variables ?
j is a member varaible. It is associated wth the instance of the class. i is a local variable and is created at the beginning of a method call and destroyed (eventually) after the method completes.
> You can make your method static by using the> synchronized keyword.I think you mean You can make your method safe by using the synchronized keyword.
> Your variable j is not static, so it cannot be used in> your static method myMethod unless you instantiate> your class. This is not quite true, you cannot access a non-static variable in a static method period
>
> > Your variable j is not static, so it cannot be used
> in
> > your static method myMethod unless you instantiate
> > your class.
>
> This is not quite true, you cannot access a non-static
> variable in a static method period
No, but through an object you can.
Each thread has its own execution stack and it is on this stack that local variables are stored.
Local Object references are also stored on the stack even though the actual instance is created on the heap in the normal way. No thread can access the local variables of another Thread directly
but is a member variable inside a static object, static? :-PIt would say it is.AND, to the original post...even a thread unsafe method will never 'corrupt' a variable, only give it the wrong value(from a design pov).rob,
Sorry The int j ;should have been static int j;I thought the behaviour of a variable in a static method will be same as static variable.