File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Threads and Synchronization and the fly likes Using static varaibles in 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 » Threads and Synchronization
Bookmark "Using static varaibles in Threads" Watch "Using static varaibles in Threads" New topic
Author

Using static varaibles in Threads

Ankur Srivastava
Ranch Hand

Joined: May 11, 2004
Posts: 62
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

Joined: Mar 06, 2001
Posts: 1209
How are you running this piece of code. I dont see the StopThread class being called from anywhere
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
You get one static variable per JVM. You can't directly access variables in a different JVM.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
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...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Ankur Srivastava
Ranch Hand

Joined: May 11, 2004
Posts: 62
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

Joined: Jun 08, 2002
Posts: 3178
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...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using static varaibles in Threads
 
Similar Threads
Reg. Some Thread basics
Using static variable to start and stop a thread
Converting threadgroup to executorservice
stopping a thread
wait()