Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using static varaibles in Threads

 
Ankur Srivastava
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you running this piece of code. I dont see the StopThread class being called from anywhere
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You get one static variable per JVM. You can't directly access variables in a different JVM.
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Ankur Srivastava
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ko Ko Naing
Ranch Hand
Posts: 3178
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic