| View previous topic :: View next topic |
| Author |
Message |
NotReallySureWhatGoesHere Expert Cheater
Reputation: -1
Joined: 20 Feb 2012 Posts: 110
|
Posted: Thu Mar 08, 2012 7:52 am Post subject: Java Concurrency |
|
|
I don't know much (if any at all) about it and am in need of it (I think). For this project I am doing I don't need to know of it in great detail, only in enough to do one thing; update a label's text.
Alright so:
| Code: | private void updateTime() {
synchronized (InterfaceDialog.class) {
lblTime.setText(String.valueOf(time));
}
} |
I'm trying to get this method to run at the same time with the main thread so it can continuously update - then again that may not even be what I need, I honestly have no idea.
Here is my main function (please note the IDE auto-added the invokeLater() content):
| Code: | public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
InterfaceDialog form = new InterfaceDialog();
form.setLocationRelativeTo(null);
form.setVisible(true);
}
});
} |
I have a StopWatch class (trying to be more OOP as suggested) and I'm not not sure how I can call a method in that class to update a label on the "main" class ??? Anyway, I did some Google'ing and that's what I came up with. I have called the updateTime() method and I get no update Suggestions (no code please - or very little as possible) as you guys provided me last topic would be very helpful. Thank you!
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Thu Mar 08, 2012 8:29 am Post subject: |
|
|
You only need concurrency if you have 2+ distinct threads running where one thread is trying to read while another is trying to write. I've only really had to use it in the case of ArrayList's where information was being deleted/added and fucking up looking at the data in another thread.
If you are trying to update the label from an external class, you could pass the instance of the label into the StopWatch class in the constructor and use it to directly modify the label. Or you could use a singleton pattern on your main instance and directly get the current instance of the class containing the label.
Also, maybe it's just how I code but that above code looks very weird o.O I typically use Eclipse and not netbeans though.
_________________
|
|
| Back to top |
|
 |
NotReallySureWhatGoesHere Expert Cheater
Reputation: -1
Joined: 20 Feb 2012 Posts: 110
|
Posted: Fri Mar 09, 2012 8:00 am Post subject: |
|
|
Thanks for the input and suggestions, however I'm not sure how to do either so I just used a Timer
Anyway here is some of the code:
| Code: | timerTask = new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
updateTime();
lblTime.setText(String.valueOf(time));
}
};
t = new Timer(StaticInfo.TIMER_DELAY, timerTask);
t.start(); |
Seems to work fine. Thanks again though.
|
|
| Back to top |
|
 |
HomerSexual Grandmaster Cheater Supreme
Reputation: 5
Joined: 03 Feb 2007 Posts: 1657
|
Posted: Fri Mar 09, 2012 12:13 pm Post subject: |
|
|
if I ever make timers I just do a simple thread
| Code: | class TimeKeeper extends Thread
{
boolean stop = false;
public TimeKeeper(...) { anything you need to pass in here }
public void run()
{
while(!stop)
{
try { Thread.sleep(durationMS); } catch(Exception e) {}
}
}
public boolean endThread() { stop = true; }
} |
_________________
|
|
| Back to top |
|
 |
NotReallySureWhatGoesHere Expert Cheater
Reputation: -1
Joined: 20 Feb 2012 Posts: 110
|
Posted: Sat Mar 10, 2012 12:18 pm Post subject: |
|
|
| HomerSexual wrote: | if I ever make timers I just do a simple thread
| Code: | class TimeKeeper extends Thread
{
boolean stop = false;
public TimeKeeper(...) { anything you need to pass in here }
public void run()
{
while(!stop)
{
try { Thread.sleep(durationMS); } catch(Exception e) {}
}
}
public boolean endThread() { stop = true; }
} |
|
Ah, that's a good idea too!
I have a question though: would the extending Thread be another thread or would it remain on the same thread as my main application? Because it would probably be better to have 2 different threads (?), however the the form doesn't ever seem to freeze or stop-working while the Timer is running and the text is being changed. Thanks again!
|
|
| Back to top |
|
 |
|