wood burning stoves 2.0*
The moose likes Java in General and the fly likes What happens to the static variable here ? Threads ! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "What happens to the static variable here ? Threads !" Watch "What happens to the static variable here ? Threads !" New topic
Author

What happens to the static variable here ? Threads !

ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Hi techie's,

I have built a code, as such

We have four java files :

thread2.java

thread1.java

SyncThread.java

threadController.java

exceuting each file in seperate command prompt.

thread1 and thread2 are files and each has a run method.

from the run method am calling the SyncMethod - synchronized method in a while loop at file SyncThread, where it just prints the values of astatic member variable SyncVariable.


ThreadController is the class that modifies the SyncVariable(Static) at SyncThread Class to 1.


but still those thread 1 and thread 2 are displaying the value to be 0

Why ?

i have give the code ?

Please execute each file in a seperate command prompt!



The threadContrroller class:

import java.io.BufferedReader;
import java.io.InputStreamReader;


class threadController extends Thread implements Runnable
{
String str = null;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

void ControlMethod()throws Exception
{
while(true)
{

System.out.println("\n\nPlease Enter a valid code : ");
str = br.readLine();

int val = Integer.parseInt(str);



if(val == 1)
{
System.out.println("Setting SyncVariable to : 1");
SyncThread.ValueModifier(1);
}

}




}


public static void main(String args[])throws Exception
{

threadController objthreadController = new threadController();

objthreadController.ControlMethod();

}
}


this is the sync thread class


class SyncThread
{
static int SyncVariable;

public static void ValueModifier(int i)
{
SyncVariable = i;
}

public static synchronized void SyncMethod()
{
try
{
System.out.print("\n\n\t Threads Execute -> "+SyncVariable);
Thread.sleep(1000);
}
catch(Exception e)
{
System.out.println("\n\n Exception e"+e);
}

}
}



import java.io.BufferedReader;
import java.io.InputStreamReader;

class thread1 extends Thread
{



public void run()
{

SyncThread objSyncThread = new SyncThread();

try
{

while(true)
{
objSyncThread.SyncMethod();
}

}
catch(Exception e)
{
e.printStackTrace();
}


}

void Starter()
{

thread1 objThread1 = new thread1();

objThread1.start();
}

public static void main(String args[])throws Exception
{
thread1 objThread1 = new thread1();

objThread1.Starter();


}

}



import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileWriter;

class thread2 extends Thread
{


public void run()
{
SyncThread objSyncThread = new SyncThread();

try
{

while(true)
{
objSyncThread.SyncMethod();
}

}
catch(Exception e)
{
e.printStackTrace();
}

}

void Starter()throws Exception
{
thread2 objThread2 = new thread2();

objThread2.start();
}

public static void main(String args[])throws Exception
{
thread2 objThread2 = new thread2();

objThread2.Starter();


}

}

Now execute this in seperate command prompts.

Why it did not show 1 when i modify the value


Discussion - the powerfull way to excellence!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41178
    
  45
I haven't gone through the code - it's too hard to make sense of; please UseCodeTags- but from your last sentence it seems that you expect static variables to be the same across different JVMs; is that what you're asking?

If so, no synchronization happens between different JVMs running on the same machine. That means that static variables can (or will) have different values. Only within a single JVM will the value be the same.

(And actually, to confuse things further, a class can be loaded by more than one classloader within a single JVM. In that case, the same static variable can have different values even within a JVM, because the classes are considered to be different. I wouldn't mention that in the beginner's forum, but since this is intermediate, it seems worth a mention.)


Ping & DNS - my free Android networking tools app
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Thanks for the response,

by the way do you mean to say that,

when i run java programs in 2 command prompts it runs in 2 seperate JVM's ?





this is the sync thread class










Now execute this in seperate command prompts.

Why it did not show 1 when i modify the value with the method call as :

SyncThread.ValueModifier(1) - in the ThreadController class

*i ran each program in seperate command prompts


[ June 26, 2008: Message edited by: ram kumar ]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
Executing each program in a separate commmand prompt is almost the same as executing them on separate machines or executing them one at a time on the same machine. To communicate between JVMs, the programs could write/read a file or use Sockets, otherwise the programs running in JVMs are separate and unique.
ram kumar
Ranch Hand

Joined: May 22, 2008
Posts: 146
Originally posted by Norm Radder:
Executing each program in a separate commmand prompt is almost the same as executing them on separate machines or executing them one at a time on the same machine. To communicate between JVMs, the programs could write/read a file or use Sockets, otherwise the programs running in JVMs are separate and unique.


that was cool and an answer that i was doubting for 3 years in this field.

Thanks for clearing up!

But for each program a copy of the JVM is maintatined or how it works ?

So the program has executed properly ! I have to modify that accordingly to make the changes.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What happens to the static variable here ? Threads !
 
Similar Threads
Question on Threads
join() method usage
Threading issue
Using wait() and notify()
locking??