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!
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: 35243
7
posted
0
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.)
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: 681
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: What happens to the static variable here ? Threads !