Forums Register Login

Using static varaibles in Threads

+Pie Number of slices to send: Send
Hi,

I am trying to use a static vairable in my thread class to start and stop the thread by setting the variable true/false, but this does not seems to work.

I am attaching the code below:

//Thread Class
class MyThread extends Thread
{
volatile static boolean status = false;
long duration;

MyThread()
{
this.duration = 1000;
}

MyThread(long duration)
{
this.duration = duration;
}
public void run()
{
while (status)
{
System.out.println("MyThread::run::Going to sleep !!");
try
{
sleep(duration);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("MyThread::run::Woke Up !!");
}

}
public static void setStatus(boolean flag)
{
status = flag;
}
}

//I use this class to start the thread
class StartThread
{
public static void main(String s[])
{
MyThread th = new MyThread(1000);
MyThread.setStatus(true);
th.start();
System.out.println("MyThread::status::After Thread start:: "+MyThread.status);
}

}

//Use this class to stop the thread
class StopThread
{
public static void main(String s[])
{
MyThread.setStatus(false);
}
}

I might have missed out a very basic stuff somewhere in my code. From my understanding this should work, but it is not

So plz. let me know where am I wrong.

Thanks & Regards
Ankur
+Pie Number of slices to send: Send
How are you running this piece of code. I dont see the StopThread class being called from anywhere
+Pie Number of slices to send: Send
You get one static variable per JVM. You can't directly access variables in a different JVM.
+Pie Number of slices to send: Send
 

Originally posted by Ilja Preuss:
You get one static variable per JVM. You can't directly access variables in a different JVM.



I agree with Ilja Preuss... You cannot control a thread's static variable, which is tied to a particular JVM from another JVM... You might need to change your program design to fulfil the program's requirements in another way...
+Pie Number of slices to send: Send
Thanks a lot it seems I have figured out the problem. I was using the command prompt to run the two different classes. So there will be a seperate instance of JVM's invoked to run each of these classes. Am I right?

But if I put both the classes StartThread and StopThread in jar file, and use the jar file in my web application then they will be executed in a common JVM. Isn't it??

Thanks & Regards
Ankur
+Pie Number of slices to send: Send
 

Originally posted by Ankur Srivastava:
[QB]Thanks a lot it seems I have figured out the problem. I was using the command prompt to run the two different classes. So there will be a seperate instance of JVM's invoked to run each of these classes. Am I right?


Yes, you are right! You may run the two classes from only one JVM so that the static variable accessing is the same...


But if I put both the classes StartThread and StopThread in jar file, and use the jar file in my web application then they will be executed in a common JVM. Isn't it??


Yes, absolutely...
Danger, 10,000 volts, very electic .... tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 973 times.
Similar Threads
Reg. Some Thread basics
wait()
stopping a thread
Using static variable to start and stop a thread
Converting threadgroup to executorservice
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 00:18:51.