• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

What happens to the static variable here ? Threads !

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.)
 
ram kumar
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Master Rancher
Posts: 5112
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
And when my army is complete, I will rule the world! But, for now, I'm going to be happy with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic