Reputation: 230
I've tried creating a public static variable (NOT LOCAL) and purposely making an increment to it and telling Java:
"If this variable == 0, then execute this code"
so that even if that method is called the second time, that block of code won't execute because the variable has changed and is no longer zero... and it will never again be zero because it keeps increasing.
public void actionPerformed(ActionEvent e){
if(e.getSource()==deal){/*do something*/}
}
My problem is that the if statment executes more than once when I press the button "deal".
Upvotes: 1
Views: 5461
Reputation: 2611
Try this way, It's dummy code but through this way, you can execute inner for loop
code only once.
List<WebElement> allelements = driver.findElements(By.id("id1"));
int i = 0;
for (WebElement e : allelements)
{
i++;
List<WebElement> secondelements = driver.findElements(By.id("id2"));
if(i==1)
{
for(WebElement ae : secondelements)
{
System.out.println(ae.getText());
}
}
System.out.println(e.getText());
}
Upvotes: 0
Reputation: 2985
Try something like:
public class Test {
private boolean isExecuted;
public synchronized void executeOnce() {
if (isExecuted) {
return;
} else {
//do your stuff
isExecuted = true;
}
}
}
Modify it as per your requirement. To improve performance, you can use double checked locking.
Upvotes: 3