I'm trying to have one thread keep track of the time while the main thread does the work.. For some reason the thread is not getting the updated variable, how do I get this to work? I tried using a static variable but it's not working..
I'm trying to share these two variables in the run method:
static boolean timeIsUp = false;
static boolean testFinished = false;
in the paintComponent method I adjust testFinished.. and I change the timeIsup inside the timer class. After these values are changed to true.. inside the run() while loop, it still shows the variables as both false..
Here is the code:
Some more information would be helpful. Let's start with what it is you're actually trying to accomplish, because I have a feeling you might be looking for the functionality that Swing Timers offer, but I can't be sure. In any case, you may want have a peek at the Java concurrency tutorial to learn how to safely share data between threads. It seems like the two boolean values are used as simple flags, which could make them good candicates for being declared volatile, but again let's start with what it is you're trying to accomplish.
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
The reset() method is the one which is setting timeIsUp field. But i dont see any code that is calling either reset() directly or via paintComponent(). Is it not included or its never called ?
Joined: Feb 19, 2012
Sorry for the lack of explanation.. But yes the two boolean variables are flag variables, and I'm basically setting them outside of that loop.. So I'm trying to figure out how to get those variables updated. I'm basically messing around with threads and polymorphism. And have a simple question and answer test the user takes. The test questions are set through a text file and loads the question and answers in to the array based off if it is an image question or text question..
I have this class I have posted which uses an ArrayList of another abstract class (Question) and the array is then filled with two classes (textQuestion) and (ImageQuestion) which extend (Question) class. My other class (Answer), gathers the answer from the user and when the user submits it calls repaint for QuestionPanel (the one currently posted in the forum)..
I have a timer class which implements a timer set to 1000, or seconds. and the user sets the time. When the timer is up this class sets the isTimeUp=true.. When the user sets the time and hit's submit.. it starts the timer, and fires the "go" method inside QuestionPanel (the one currently posted in the forum)
I tie all these classes together in one main class called Quiz.. which sets up the JFrame and inserts each panel, and passes objects of the three panel classes: timer, answer, and questionPanel class...
the changes to the two boolean variables reflect outside of the loop in run() when I test them, just not inside..
So the problem here is when one of the boolean variables in the QuestionPanel is changed it doesn't reflect it inside the run() method.. so I'm trying to figure out the best way to do this.. and understand it more. I will take a look at the link provided and look into the volatile setting too..
Also, if I set the boolean variables to static... shouldn't both threads be accessing the memory address of the variable? this is Why I don't get why the changes are not being reflected..
Joseph Cho wrote:Also, if I set the boolean variables to static... shouldn't both threads be accessing the memory address of the variable? this is Why I don't get why the changes are not being reflected..
Sometimes java seems to work in strange ways but most of the times just that we are not looking at the right place. For example if you execute below code, the worker thread never ends unless you make either staticStopWork or stopWork as volatile
I am not an expert in GUI stuff but your code more or less seemed to be the case above. As Jelle pointed out volatile should make it work.