Invoking Later, But Only Once

D

drcode

Hi, I'm writing a program where multiple operations may run that affect
the gui:

if(foo){
dostuff();
SwingUtilites.invokeLater(UpdateGui());
}
if(bar){
dostuff();
SwingUtilites.invokeLater(UpdateGui());
}
if(baz){
dostuff();
SwingUtilites.invokeLater(UpdateGui());
}

This might cause multiple GUI update operations to be placed on the
queue- However, I only need my GUI to be update once- Basically, if an
update is already on the queue, I want it to not bother placing another
one on there.

Clearly, there are ways to handle this programatically (have the GUI
event check if any modifications have happened since the last update)-
But I was wondering if there was a more "Javaish" way to post an event
to be executed, but not if it's already on the event queue?

Thanks for any tips!

Conrad Barski, M.D.
 
A

Andrey Kuznetsov

the gui:
if(foo){
dostuff();
SwingUtilites.invokeLater(UpdateGui());
}
if(bar){
dostuff();
SwingUtilites.invokeLater(UpdateGui());
}
if(baz){
dostuff();
SwingUtilites.invokeLater(UpdateGui());
}
This might cause multiple GUI update operations to be placed on the
queue- However, I only need my GUI to be update once- Basically, if an
update is already on the queue, I want it to not bother placing another
one on there.

int count;

public void addUpdate() {
count++;
SwingUtilites.invokeLater(UpdateGui());
}

public void doUpdate() {
if(count > 0) {
count = 0;
UpdateGui();
}
}
 
A

Andrey Kuznetsov

int count;
public void addUpdate() {
count++;
SwingUtilites.invokeLater(UpdateGui());
}

public void doUpdate() {
if(count > 0) {
count = 0;
UpdateGui();
}
}

sorry, that was wrong...

here is right one:


int count;

public void addUpdate() {
count++;
SwingUtilites.invokeLater(doUpdate());
}

public void doUpdate() {
if(count > 0) {
count = 0;
UpdateGui();
}
}
 
D

drcode

hmm... I like this approach- It's quite a bit simpler than what I was
thinking, but still looks like it will work- Thanks!
 
A

Andrey Kuznetsov

hmm... I like this approach- It's quite a bit simpler than what I was
thinking, but still looks like it will work- Thanks!

yeah, to keep it simple is the most important thing
 
C

Chris Uppal

Andrey said:
int count;

public void addUpdate() {
count++;
SwingUtilites.invokeLater(doUpdate());
}

Just to add, in case it's not already obvious:

Since count is being accessed from two threads, you should have some
synchonisation in there too.

-- chris
 
A

Andrey Kuznetsov

int count;
Just to add, in case it's not already obvious:

Since count is being accessed from two threads, you should have some
synchonisation in there too.
in worst case you'll get 2 gui updates instead of 1. Does it really matters?
And if gui update takes longer then it is rather unlikely that you get it
twice!
 
C

Chris Uppal

Andrey said:
in worst case you'll get 2 gui updates instead of 1. Does it really
matters?

In the worst case the change made to count by one thread will never be visible
to the other thread at all.

-- chris
 
A

Andrey Kuznetsov

Since count is being accessed from two threads, you should have some
In the worst case the change made to count by one thread will never be
visible
to the other thread at all.

hmm, then make count volatile and don't worry about it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top