1
votes

I had a suspect that a bug in one of my Codename One projects was caused by concurrent executions of the same listener (if the user taps a button very quickly more times, invoking its listener before it ended the execution)... I added a lock variable in the code, to avoid multiple executions at the same time, and this solved the bug.

This is my first time that I have this kind of problem. Reading on the web, it's suggested to use the synchronized Java keyword (however I'm not sure if it can be useful in this case).

My question is if the synchronized Java keyword is supported by Codename One.

2

2 Answers

2
votes

synchronized works fine in Codename One but if you used an action listener it's unlikely that it solved the issue unless we have a huge unimaginable bug.

All events, paints, lifecycle methods etc. are invoked on the EDT. It's a single thread so two clicks on the button will happen on a single thread. synchronized would be meaningless. The EDT is used from the touch screen interaction all the way down to the event on the component itself and you can test that through the isEDT() method.

A more likely scenario is that one of the action listeners on the button uses invokeAndBlock which can trigger weird side effects in the event dispatch chain. invokeAndBlock() is used internally by AndWait methods, dialogs etc.

-1
votes

Using syncronized will prevent concurrent execution of the method, but will essentially queue up the request that are made, by forcing threads to wait for any current execution.

When handling this scenario, you might want to debounce the button clicks, by preventing user interaction for some period after it is first pressed, or for the duration of the resulting computation, by disabling the button and re-enabling it