I have some troubles.
For example, I have two actions: First and Second.
I have written simple utility, that uses executor service to send 100000 asynchronous requests to action First and to actions Second.
In First action I do:
HitCounter.increment();
ActionContext.getContext().getSession().put("counter", HitCounter.getAtomicCounter());
return Action.SUCCESS;
In second action I do:
System.out.println("From session: "+ActionContext.getContext().getSession().get("counter"));
System.out.println("Actual:"+ HitCounter.getAtomicCounter());
return Action.SUCCESS;
And the output I see(and it really makes me mad):
From session: 2
Actual: 69352
After some time when I use this Fitst action/Second action only from my browsers and no concurrent requests come(generated by my load utility), results "are stabilized" to actual values. Thus, I have concurrency issues.
Is there a standard way that I should use to avoid problems with concurrency in Struts2 ?
P.S. HitCounter is thread safe, because it contains only one field and it's AtomicInteger.
P.P.S. HitCounter realisation:
public class HitCounter {
private static AtomicInteger counter = new AtomicInteger(0);
public static void increment() {
counter.incrementAndGet();
}
public static int getAtomicCounter() {
return counter.get();
}
}
P.P.P.S. I removed Thread.yield(); but it didn't help. :(